Hi, before I tried to resolve an issue the method of exporting the results of a search to an excel file was working fine. The problem I had was that in the database some of the values where numeric, which when exporting to the excel file wasn’t good enough, so am trying to resolve this by doing a match within the select statement, and again as you’ll see below this works fine, as I have used it on another export, but I am having trouble with this export and I cant work it out but there are some errors.
This is on the main page -
$queryb = "SELECT Intranet.ID, Contract_Number, Contract_Status, date_Cancelled, Company_Name, Invoice_Street, Invoice_City, Invoice_State,
m2.Country_Name as Invoice_Country, Invoice_Postcode, Invoice_Tax_Code, Invoice_Telephone, Invoice_Fax, Invoice_Contact_Name, Invoice_Email, Annual_Contract_Value, Invoicing_Profile, Invoice_Paid_By,
m3.Currency as Currency_of_Contract, Contract_Start, Contract_End, Renewal_Date, Contract_Length, Quote_Number, Site_Name, Street, City, State,
m1.Country_Name as Country, Postcode, Establishment.Establishment_Name as Type_of_Establishment, No_of_Rooms, Groups.Group_Names as Group_Name, Invoice_Value,
m4.Currency as Invoice_Currency, Payment_Contact, Payment_Contact_Tel, Invoice_Frequency, Seasonal_or_Full, Month_Opens, Month_Closes,
m5.Frequency_Name as Brandcheck_Frequency,
m6.Frequency_Name as StandardsCheck_Frequency, m7.Frequency_Name as Roomcheck_Frequency, m8.Frequency_Name as Foodcheck_Frequency,
m9.Frequency_Name as Foodcheck_Frequency,
m10.Frequency_Name as Poolcheck_Frequency, m11.Frequency_Name as Firecheck_Frequency, m12.Frequency_Name as Aquacheck_Frequency, m13.Frequency_Name as Spacheck_Frequency, m14.Frequency_Name as Safetycheck_Frequency, m15.Frequency_Name as Accesscheck_Frequency, m16.Frequency_Name as Ecocheck_Frequency, m17.Frequency_Name as Supplycheck_Frequency, m18.Frequency_Name as Dinecheck_Frequency, m19.Frequency_Name as Tourcheck_Frequency, m20.Frequency_Name as Securitycheck_Frequency,
m21.Frequency_Name as Restaurantcheck_Frequency, Training_Days, m22.Frequency_Name as Labcheck_Aqua_Frequency, m23.Frequency_Name as Labcheck_Food_Frequency,
m24.Frequency_Name as Labcheck_Pool_Frequency, m25.Frequency_Name as Labcheck_Room_Frequency, m26.Frequency_Name as Legionella_Test_Frequency, Additional_Sampling,
No_of_Modules, Principle_Contact, Principle_Telephone, Preferred_Language, Principle_Contact_Job_Title, Principle_Contact_Email, contract_File, date_Created, date_Modified,
Date_Disabled, ROW_NUMBER() OVER (ORDER BY Intranet.ID) AS 'RowNumber' FROM Intranet LEFT JOIN Currency AS m3 ON (Intranet.Currency_of_Contract = m3.ID) LEFT JOIN Currency AS
m4 ON (Intranet.Invoice_Currency = m4.ID) LEFT JOIN Country AS m1 ON (Intranet.Country = m1.ID) LEFT JOIN Country AS m2 ON (Intranet.Invoice_Country = m2.ID) LEFT JOIN Establishment ON (Intranet.Type_of_Establishment = Establishment.ID) LEFT JOIN Groups ON (Intranet.Group_Name = Groups.ID) LEFT JOIN Frequency AS m5 ON (Intranet.Brandcheck_Frequency = m5.ID)
LEFT JOIN Frequency AS m6 ON (Intranet.StandardsCheck_Frequency = m6.ID) LEFT JOIN Frequency AS m7 ON (Intranet.Roomcheck_Frequency = m7.ID) LEFT JOIN Frequency AS m8 ON (Intranet.Foodcheck_Frequency = m8.ID)
LEFT JOIN Frequency AS m9 ON (Intranet.Foodcheck_Frequency = m9.ID) LEFT JOIN Frequency AS m10 ON (Intranet.Poolcheck_Frequency = m10.ID) LEFT JOIN Frequency AS m11 ON (Intranet.Firecheck_Frequency = m11.ID) LEFT JOIN Frequency AS m12 ON (Intranet.Aquacheck_Frequency = m12.ID) LEFT JOIN Frequency AS m13 ON (Intranet.Spacheck_Frequency = m13.ID) LEFT JOIN Frequency AS m14 ON (Intranet.Spacheck_Frequency = m14.ID) LEFT JOIN Frequency AS m15 ON (Intranet.Accesscheck_Frequency = m15.ID) LEFT JOIN Frequency AS m16 ON (Intranet.Ecocheck_Frequency = m16.ID) LEFT JOIN Frequency AS m17 ON (Intranet.Ecocheck_Frequency = m17.ID)
LEFT JOIN Frequency AS m18 ON (Intranet.Dinecheck_Frequency = m18.ID) LEFT JOIN Frequency AS m19 ON (Intranet.Dinecheck_Frequency = m19.ID) LEFT JOIN Frequency AS m20 ON (Intranet.Securitycheck_Frequency = m20.ID)
LEFT JOIN Frequency AS m21 ON (Intranet.Restaurantcheck_Frequency = m21.ID) LEFT JOIN Frequency AS m22 ON (Intranet.Labcheck_Aqua_Frequency = m22.ID) LEFT JOIN Frequency AS m23 ON (Intranet.Labcheck_Food_Frequency = m23.ID) LEFT JOIN Frequency AS m24 ON (Intranet.Labcheck_Pool_Frequency = m24.ID) LEFT JOIN Frequency AS m25 ON (Intranet.Labcheck_Room_Frequency = m25.ID) LEFT JOIN Frequency AS m26 ON (Intranet.Legionella_Test_Frequency = m26.ID) WHERE {limit clause} order by ID DESC";
$_SESSION['currentQuery'] = $queryb;
And this is sent to a file called download_2.php and it’s here where I am getting the following errors.
<b>Fatal error</b>: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: [Microsoft][ODBC Driver 11 for SQL Server]Syntax error
permission violation or other nonspecific error' in \\CSFFILES11\WEBSITES\live\new_checksafetyfirst\en\csfintranet\download_2.php:91
Stack trace:
0 \\CSFFILES11\WEBSITES\live\new_checksafetyfirst\en\csfintranet\download_2.php(91): PDO->prepare('SELECT Intranet...')
1 {main} thrown in <b>\\CSFFILES11\WEBSITES\live\new_checksafetyfirst\en\csfintranet\download_2.php</b> on line <b>91</b><br />
function cleanData(&$str)
{
if($str == 't') $str = 'TRUE';
if($str == 'f') $str = 'FALSE';
if($str == "'") $str = "\\'";
if(preg_match("/^0/", $str) || preg_match("/^\+?\d{8,}$/", $str) || preg_match("/^\d{4}.\d{1,2}.\d{1,2}/", $str)) {
$str = "'$str";
}
if(strstr($str, '"')) $str = '"' . str_replace('"', '""', $str) . '"';
$str = mb_convert_encoding($str, 'UTF-16LE', 'UTF-8');
}
// filename for download
$filename = "website_data_" . date('Ymd') . ".csv";
header("Content-Disposition: attachment; filename=\"$filename\"");
header("Content-Type: text/csv; charset=UTF-16LE");
$out = fopen("php://output", 'w');
$flag = false;
$row=0;
$conn->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$result = $conn->prepare($query);
echo ($result);
if (strpos($query, ":trimmed1") !== false) {$result->bindParam(':trimmed1', $trimmed); }
if (strpos($query, ":trimmed2") !== false) {$result->bindParam(':trimmed2', $trimmed); }
if (strpos($query, ":trimmed3") !== false) {$result->bindParam(':trimmed3', $trimmed); }
if (strpos($query, ":trimmed4") !== false) {$result->bindParam(':trimmed4', $trimmed); }
if (strpos($query, ":trimmed5") !== false) {$result->bindParam(':trimmed5', $trimmed); }
if (strpos($query, ":trimmed6") !== false) {$result->bindParam(':trimmed6', $trimmed); }
if (strpos($query, ":trimmed7") !== false) {$result->bindParam(':trimmed7', $trimmed); }
if (strpos($query, ":trimmed8") !== false) {$result->bindParam(':trimmed8', $trimmed); }
if (strpos($query, ":trimmed9") !== false) {$result->bindParam(':trimmed9', $trimmed); }
if (strpos($query, ":trimmed10") !== false) {$result->bindParam(':trimmed10', $trimmed); }
if (strpos($query, ":trimmed11") !== false) {$result->bindParam(':trimmed11', $trimmed); }
if (strpos($query, ":trimmed12") !== false) {$result->bindParam(':trimmed12', $trimmed); }
if (strpos($query, ":trimmed13") !== false) {$result->bindParam(':trimmed13', $trimmed); }
if (strpos($query, ":trimmed14") !== false) {$result->bindParam(':trimmed14', $trimmed); }
if (strpos($query, ":trimmed15") !== false) {$result->bindParam(':trimmed15', $trimmed); }
if (strpos($query, ":trimmed16") !== false) {$result->bindParam(':trimmed16', $trimmed); }
if (strpos($query, ":trimmed17") !== false) {$result->bindParam(':trimmed17', $trimmed); }
if (strpos($query, ":trimmed18") !== false) {$result->bindParam(':trimmed18', $trimmed); }
if (strpos($query, ":trimmed19") !== false) {$result->bindParam(':trimmed19', $trimmed); }
if (strpos($query, ":trimmed20") !== false) {$result->bindParam(':trimmed20', $trimmed); }
if (strpos($query, ":trimmed21") !== false) {$result->bindParam(':trimmed21', $trimmed); }
if (strpos($query, ":trimmed22") !== false) {$result->bindParam(':trimmed22', $trimmed); }
if (strpos($query, ":trimmed23") !== false) {$result->bindParam(':trimmed23', $trimmed); }
if (strpos($query, ":trimmed24") !== false) {$result->bindParam(':trimmed24', $trimmed); }
if (strpos($query, ":trimmed25") !== false) {$result->bindParam(':trimmed25', $trimmed); }
if (strpos($query, ":trimmed26") !== false) {$result->bindParam(':trimmed26', $trimmed); }
if (strpos($query, ":trimmed27") !== false) {$result->bindParam(':trimmed27', $trimmed); }
if (strpos($query, ":trimmed28") !== false) {$result->bindParam(':trimmed28', $trimmed); }
if (strpos($query, ":trimmed29") !== false) {$result->bindParam(':trimmed29', $trimmed); }
if (strpos($query, ":trimmed30") !== false) {$result->bindParam(':trimmed30', $trimmed); }
if (strpos($query, ":trimmed31") !== false) {$result->bindParam(':trimmed31', $trimmed); }
if (strpos($query, ":trimmed32") !== false) {$result->bindParam(':trimmed32', $trimmed); }
if (strpos($query, ":trimmed33") !== false) {$result->bindParam(':trimmed33', $trimmed); }
if (strpos($query, ":trimmed34") !== false) {$result->bindParam(':trimmed34', $trimmed); }
if (strpos($query, ":trimmed35") !== false) {$result->bindParam(':trimmed35', $trimmed); }
if (strpos($query, ":trimmed36") !== false) {$result->bindParam(':trimmed36', $trimmed); }
if (strpos($query, ":trimmed37") !== false) {$result->bindParam(':trimmed37', $trimmed); }
if (strpos($query, ":trimmed38") !== false) {$result->bindParam(':trimmed38', $trimmed); }
if (strpos($query, ":trimmed39") !== false) {$result->bindParam(':trimmed39', $trimmed); }
if (strpos($query, ":trimmed40") !== false) {$result->bindParam(':trimmed40', $trimmed); }
if (strpos($query, ":trimmed41") !== false) {$result->bindParam(':trimmed41', $trimmed); }
if (strpos($query, ":trimmed42") !== false) {$result->bindParam(':trimmed42', $trimmed); }
if (strpos($query, ":trimmed43") !== false) {$result->bindParam(':trimmed43', $trimmed); }
if (strpos($query, ":trimmed44") !== false) {$result->bindParam(':trimmed44', $trimmed); }
if (strpos($query, ":trimmed45") !== false) {$result->bindParam(':trimmed45', $trimmed); }
if (strpos($query, ":contractLike") !== false) {$result->bindParam(':contractLike', $contractLike); }
$result->execute();
foreach ($result as $row) {
if(!$flag) {
$cheaders = array();
foreach ($row as $key=>$data) {
if ($key == "ID") $key = "id";
$cheaders[] = $key;
}
fputcsv($out, array_values($cheaders), ',', '"');
$flag = true;
}
foreach ($row as $key => $value)
{
if ($value instanceof DateTime)
{
$row[$key] = date_format($value, 'd/m/y');
}
}
$row = str_replace(",", ",", $row);
$row = str_replace(".", ".", $row);
$row = str_replace("'", "'", $row);
fputcsv($out, array_values($row), ',', '"');
}
fclose($out);
exit;