This is my script. Previously was fine but after I change to PDO will pop up error message like this:
It's showing this two line got error:
stripslashes($result->getColumnMeta($i))) . $csv_enclosed;
and :
if ($row[$j] == '0' || $row[$j] != '')
Here is my script:
if(isset($_SESSION['fromdate']) && isset($_SESSION['todate'])){
$filename = 'Sales_Report.csv';
$csv_terminated = "\n";
$csv_separator = ",";
$csv_enclosed = '"';
$csv_escaped = "\\";
$result = $db->db->prepare("SELECT order.ID,order_status.TotalPrice,order_status.TransCharges,order_status.ShipCharges
FROM `order`
INNER JOIN `order_status` ON order.OrderID = order_status.OrderID
WHERE order_status.ShopID = :user AND order.Date BETWEEN :from AND :to AND w_order.Status='2'");
// Gets the data from the database
$result->bindValue(':user',$_SESSION['sesuserid']);
$result->bindValue(":from",$_SESSION['fromdate'].' 00:00:00');
$result->bindValue(":to",$_SESSION['todate'].' 23:59:59');
$result->execute();
$fields_cnt =$result->columnCount();
$schema_insert = '';
for ($i = 0; $i < $fields_cnt; $i++)
{
$l = $csv_enclosed . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed,
stripslashes($result->getColumnMeta($i))) . $csv_enclosed;
$schema_insert .= $l;
$schema_insert .= $csv_separator;
} // end for
$out = trim(substr($schema_insert, 0, -1));
$out .= $csv_terminated;
// Format the data
while ($row = $result->fetch(PDO::FETCH_ASSOC))
{
$schema_insert = '';
for ($j = 0; $j < $fields_cnt; $j++)
{
if ($row[$j] == '0' || $row[$j] != '')
{
if ($csv_enclosed == '')
{
$schema_insert .= $row[$j];
} else
{
$schema_insert .= $csv_enclosed .
str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, $row[$j]) . $csv_enclosed;
}
} else
{
$schema_insert .= '';
}
if ($j < $fields_cnt - 1)
{
$schema_insert .= $csv_separator;
}
} // end for
$out .= $schema_insert;
$out .= $csv_terminated;
} // end while
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Length: " . strlen($out));
// Output to browser with appropriate mime type, you choose ;)
header("Content-type: text/x-csv");
//header("Content-type: text/csv");
//header("Content-type: application/csv");
header("Content-Disposition: attachment; filename=$filename");
echo $out;
exit;
}else{
echo "<SCRIPT LANGUAGE='JavaScript'>
window.alert('Please Select Date Range Before Execute Export Function.')
window.location.href='Location: http://".$_SERVER['HTTP_HOST']."/admin/merchant/salesreport.php';
</SCRIPT>";
}