This is a new project but I’m trying to use code that has worked well before, the difference being that the output needed is far less and its using mysql this time and sql last time.
I have a detail page, and the relevant code on that page is below:
<?
$a=mysql_query("select * from hazzard where UniqueIdentifier = ".$report_ID."");
$_SESSION['currentQuery'] = $a;
$data=mysql_fetch_assoc($a);
?>
I have a button on this page as below:
<a href="download_2.php">Export This Report (Excel/.csv)</a>
Then on the download page I have the following, I have left the commented code in there as I dont think I need it, but I wasnt 100% sure to delete it.
<?
error_reporting(E_ALL);
ini_set('display_errors','On');
session_start();
header("Pragma: public"); // required
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false); // required for certain browsers
header("Content-Transfer-Encoding: binary");
header("Content-Type: application/csv");
require_once('../config.php');
$connectionInfo = array("Database"=>$db, "UID"=>$user, "PWD"=>$pass);
$conn = mysql_connect($host, $connectionInfo);
//$username=$_SESSION['user1'];
//if ($_SESSION['flaglog']!=1)
//{
//header("location:index.php");
//}
//else
//{
//$query = "SELECT * FROM Users WHERE Username='$username'";
//$res = sqlsrv_query($conn, $query);
//while ($result = sqlsrv_fetch_array($res, SQLSRV_FETCH_ASSOC)) {
//$name = $result["First_Name"];
//$security = $result["Admin"];
////echo $security;
//}
//$contractLike = substr($username, 0, 3);
//echo $contractLike;
//}
$query = $_SESSION['currentQuery'];
//var_dump($query);
//$query = str_replace('WITH LIMIT AS( ', '', $query);
//$query = str_replace(", ROW_NUMBER() OVER (ORDER BY ID) AS 'RowNumber'", '', $query);
//$query = str_replace(", ROW_NUMBER() OVER (ORDER BY Contract_Number) AS 'RowNumber'", '', $query);
//$query = str_replace('select * from LIMIT WHERE {limit clause}', '', $query);
//$query = str_replace('))', ')', $query);
//$query = str_replace('SELECT * FROM Intranet)', 'SELECT * FROM Intranet', $query);
//$query = str_replace("%')", "%'", $query);
////var_dump($query);
function cleanData(&$str)
{
if($str == 't') $str = 'TRUE';
if($str == 'f') $str = 'FALSE';
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;
$result = mysql_query($conn, $query) or die('Query failed!');
while(false !== ($row = mysql_fetch_array($result, mysql_fetch_assoc))) {
if(!$flag) {
// display field/column names as first row
fputcsv($out, array_keys($row), ',', '"');
$flag = true;
}
//array_walk($row, 'cleanData');
//start
foreach ($row as $key => $value)
{
if ($value instanceof DateTime)
{
$row[$key] = date_format($value, 'd/m/y');
}
}
//end
$row = str_replace(",", ",", $row);
$row = str_replace(".", ".", $row);
fputcsv($out, array_values($row), ',', '"');
}
fclose($out);
exit;
?>
I get the option to save as the .csv file that bit seems to be working, but when I open the csv file up I get the following errors.
<br />
<b>Warning</b>: mysql_connect() expects parameter 2 to be string
array given in <b>/home/linweb22/w/whhazardreport.co.uk/user/htdocs/manager_Admin/download_2.php</b> on line <b>15</b><br />
<br />
<b>Notice</b>: Undefined index: currentQuery in <b>/home/linweb22/w/whhazardreport.co.uk/user/htdocs/manager_Admin/download_2.php</b> on line <b>35</b><br />
<br />
null given in <b>/home/linweb22/w/whhazardreport.co.uk/user/htdocs/manager_Admin/download_2.php</b> on line <b>68</b><br />
Query failed!
Here is the link to the project im working on:
http://www.whhazardreport.co.uk/manager_Admin/detail_Ref.php?ID=75
Is it that the session hasnt got the info it requires?