downloading db records in csv but getting blank rows - php

Guys this my code downloads records from mysql as csv but wheresoever in rows there is any value with a comma , it does not download and i get a blank row instead
what could be this solution for this
here is my code
require_once('config.php');
$u =$_REQUEST['u'];
$cs =$_REQUEST['cs'];
$y =$_REQUEST['y'];
$d =$_REQUEST['d'];
$m =$_REQUEST['m'];
$date = "$y-$m-$d";
$todays = date("d-m-Y");
header('Content-Type: text/csv');
header('Content-Disposition: attachment;filename=Mortgage-'.$u.'-Records-'.$date.'.csv');
//select table to export the data
$select_table=mysql_query("SELECT * FROM records WHERE user ='".$u."' AND status ='".$cs."' AND DATE_FORMAT(posted, '%Y-%m-%d') = '$date' ORDER BY id DESC");
$rows = mysql_fetch_assoc($select_table);
if ($rows)
{
getcsv(array_keys($rows));
}
while($rows)
{
getcsv($rows);
$rows = mysql_fetch_assoc($select_table);
}
// get total number of fields present in the database
function getcsv($no_of_field_names)
{
$separate = '';
// do the action for all field names as field name
foreach ($no_of_field_names as $field_name)
{
if (preg_match('/\\r|\\n|,|"/', $field_name))
{
$field_name = '' . str_replace('', $field_name) . '';
}
echo $separate . $field_name;
//sepearte with the comma
$separate = ',';
}
//make new row and line
echo "\r\n";
}
really appreciate your time and help

Change your first part of the code like this:
require_once('config.php');
$u =$_REQUEST['u'];
$cs =$_REQUEST['cs'];
$y =$_REQUEST['y'];
$d =$_REQUEST['d'];
$m =$_REQUEST['m'];
$date = "$y-$m-$d";
$todays = date("d-m-Y");
header('Content-Type: text/csv');
header('Content-Disposition: attachment;filename=Mortgage-'.$u.'-Records-'.$date.'.csv');
//select table to export the data
$select_table=mysql_query("SELECT * FROM records WHERE user ='".$u."' AND status ='".$cs."' AND DATE_FORMAT(posted, '%Y-%m-%d') = '$date' ORDER BY id DESC");
$i = 0;
while($rows = mysql_fetch_assoc($select_table);)
{
if($i === 0) {
getcsv(array_keys($rows));
}
getcsv($rows);
$i++;
}
And do this change in function getcsv - replace this code:
$field_name = '' . str_replace('', $field_name) . '';
with this:
$field_name = '"' . $field_name . '"';
But instead of using mysql_query try to implement PDO because mysql lib is prune to mysql injection and will is deprecated in new php versions.

Related

download csv but rows with comma is blank

guys i got this codes that does work but some rows in database which has values with , commas those rows gets downloaded blank. what is the fix for this?
here is my php
<?php
require_once('config.php');
$y = $_REQUEST['y'];
$m = $_REQUEST['m'];
$date = "$y-$m";
header('Content-Type: text/csv');
header('Content-Disposition: attachment;filename=Data-Backup-' . $date . '.csv');
$select_table = mysql_query("SELECT * FROM records WHERE DATE_FORMAT(data_submitted, '%Y-%m') = '$date' ORDER BY ID DESC");
$rows = mysql_fetch_assoc($select_table);
if ($rows) {
getcsv(array_keys($rows));
}
while ($rows) {
getcsv($rows);
$rows = mysql_fetch_assoc($select_table);
}
function getcsv($no_of_field_names)
{
$separate = '';
foreach ($no_of_field_names as $field_name) {
if (preg_match('/\\r|\\n|,|"/', $field_name)) {
$field_name = '' . str_replace('', $field_name) . '';
}
echo $separate . $field_name;
$separate = ',';
}
echo "\r\n";
}
?>
You can use fputcsv.
$output = fopen('php://output', 'w');
$count = 0;
while($row = mysql_fetch_assoc($select_table)) {
if ($count == 0) {
// header
fputcsv($output, array_keys($row));
}
fputcsv($output, array_values($row));
$count++;
}
fpassthru($output);

Php mysql Get&Post query to excel

I have a page that posts to index page like
index.php?coursecode=COURSECODEHERE
My non-variable query works well. Gives me all table as excel file.
I need to use this query with variables.
<?php
require_once('dbconnect.php');
$setCounter = 0;
$setExcelName = "download_excal_file";
$setSql = "SELECT * FROM courses WHERE coursecode POSTED VARIABLE COURSECODE HERE";
$setRec = mysql_query($setSql);
$setCounter = mysql_num_fields($setRec);
for ($i = 0; $i < $setCounter; $i++) {
$setMainHeader .= mysql_field_name($setRec, $i)."\t";
}
while($rec = mysql_fetch_row($setRec)) {
$rowLine = '';
foreach($rec as $value) {
if(!isset($value) || $value == "") {
$value = "\t";
} else {
//It escape all the special charactor, quotes from the data.
$value = strip_tags(str_replace('"', '""', $value));
$value = '"' . $value . '"' . "\t";
}
$rowLine .= $value;
}
$setData .= trim($rowLine)."\n";
}
$setData = str_replace("\r", "", $setData);
if ($setData == "") {
$setData = "\n no matching records found\n";
}
$setCounter = mysql_num_fields($setRec);
//This Header is used to make data download instead of display the data
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=".$setExcelName."_Reoprt.xls");
header("Pragma: no-cache");
header("Expires: 0");
//It will print all the Table row as Excel file row with selected column name as header.
echo ucwords($setMainHeader)."\n".$setData."\n";
?>
How can i do that ?
1.- mysql_ is deprecated, switch to PDO or mysqli ASAP
2.- Always sanitize the input, you never know who may call your link and insert stuff (or delete stuff) from your database.
Said that, you can do something like:
$setSql = "SELECT * FROM courses WHERE
coursecode LIKE '".sanitizeThisShit($_GET["coursecode"])."'";

PHP Export to CSV does not send all data to the file

I am exporting to CSV from my php reporting website. I have reports that are more than 80k rows. When I export one of them the whole data set does not get exported. I have tried several times with a report that is 88k+ rows and it exports 87k+ rows and then stops part way through the last row that is exported.
What is going on? The query that pulls the data from MSSQL is correct (I've checked).
Here's my Export file:
error_reporting(E_ALL);
ini_set('display_errors', 1);
include('DBConn.php');
include 'Helper/LogReport.php';
if(isset($_GET['Id']))
{
$id = $_GET['Id'];
$query = $conn->prepare('SELECT QName, tsql from pmdb.QDefs WHERE Id = ' . $id);
$query->execute();
$qdef = $query->fetch(PDO::FETCH_ASSOC);
}
else
{
$query = $conn->prepare("SELECT QName, tsql from pmdb.QDefs WHERE QName = '" .$TableName. "'");
$query->execute();
$qdef = $query->fetch(PDO::FETCH_ASSOC);
}
// Create and open file for writing
$filepath = 'exports/';
$filename = $qdef['QName'] . '.csv';
try
{
header('Content-Encoding: UTF-8');
header('Content-Type: text/csv; charset:UTF-8');
header('Content-Disposition: attachment; filename="' . $filename . '"');
}
catch(Exception $e)
{
echo "Something went wrong<br>";
die( print_r( $e->getMessage()));
}
//define separators
$sep = ","; //separator
$br = "\r\n"; //line break
// Use returned tsql field as query for dataset
$tsql = $qdef['tsql'];
if(isset($DataReturn))
{
if(strpos($DataReturn['Order'],'EDIT'))
{
$DataReturn['Order'] = str_replace('EDIT','Id',$DataReturn['Order']);
}
$tsql = $tsql . $DataReturn['WhereClause'] . $DataReturn['Order'] . $DataReturn['Limit'];
}
$query = $conn->prepare($tsql);
$query->execute();
// Output data to CSV file
$headers = NULL;
while ($row = $query->fetch(PDO::FETCH_ASSOC))
{
//Write column headings to file
if (is_null($headers))
{
$headers = array_keys($row);
if ($headers[0] == 'ID')
$headers[0] = 'Id';
foreach($headers as $Header)
{
echo $Header. ",";
}
echo $br;
}
//Write data
$modRow = preg_replace('/ \d{2}:\d{2}:\d{2}\.\d{3}/', '', $row);
$modRow = preg_replace( "/\r|\n/", "", $modRow );
foreach($modRow as $RowPrint)
{
echo '"' .trim(unserialize(serialize($RowPrint))). '"' .$sep;
}
echo $br;
}
I don't see anything that would stop the data stream from completing the export.
EDIT
I have tried the fputcsv() method like this:
error_reporting(E_ALL);
ini_set('display_errors', 1);
include('DBConn.php');
include 'Helper/LogReport.php';
//print_r($_GET);
if(isset($_GET['Id']))
{
$id = $_GET['Id'];
$query = $conn->prepare('SELECT QName, tsql from pmdb.QDefs WHERE Id = ' . $id);
$query->execute();
$qdef = $query->fetch(PDO::FETCH_ASSOC);
}
else
{
$query = $conn->prepare("SELECT QName, tsql from pmdb.QDefs WHERE QName = '" .$TableName. "'");
$query->execute();
$qdef = $query->fetch(PDO::FETCH_ASSOC);
}
// Create and open file for writing
$filepath = 'exports/';
$filename = $qdef['QName'] . '.csv';
try
{
$openFile = fopen($filepath . $filename,'a');
header('Content-Encoding: UTF-8');
header('Content-Type: text/csv; charset:UTF-8');
header('Content-Disposition: attachment; filename="' . $filename . '"');
}
catch(Exception $e)
{
echo "Something went wrong<br>";
die( print_r( $e->getMessage()));
}
//define separators
$sep = ","; //separator
$br = "\r\n"; //line break
// Use returned tsql field as query for dataset
$tsql = $qdef['tsql'];
if(isset($DataReturn))
{
if(strpos($DataReturn['Order'],'EDIT'))
{
$DataReturn['Order'] = str_replace('EDIT','Id',$DataReturn['Order']);
}
$tsql = $tsql . $DataReturn['WhereClause'] . $DataReturn['Order'] . $DataReturn['Limit'];
}
$query = $conn->prepare($tsql);
$query->execute();
// Output data to CSV file
$headers = NULL;
while ($row = $query->fetch(PDO::FETCH_ASSOC))
{
//Write column headings to file
if (is_null($headers))
{
$headers = array_keys($row);
if ($headers[0] == 'ID')
$headers[0] = 'Id';
fputcsv($openFile, $headers);
}
//Write data
$modRow = preg_replace('/ \d{2}:\d{2}:\d{2}\.\d{3}/', '', $row);
$modRow = preg_replace( "/\r|\n/", "", $modRow );
fputcsv($openFile, $modRow, ',','"');//print_r($modRow);
foreach($modRow as $RowPrint)
{
error_log(date("Y/m/d h:i:sa "). ' RowPrint: ' .$RowPrint. '\n',3,'C:\Temp\LogPHP.txt');
}
echo $br;
}
// Close file
fclose($openFile);
This just creates an empty file.
UPDATE
I ran a couple of reports for one that should have 103k+ rows. They both stopped between 61000 and 62000 rows. I did a character count and they both have around 40 million. I don't see anything about a 40million character limit.
I got it working and figured out how to get the fputcsv to work.
I needed to add an exit() to the end of the export. Now it not only sends all the data fputcsv works as well. I got the answer from another question that I ask about getting fputcsv to work.

CSV export to folder with foreach function

I have an php script, which i want to use to automatically export data from a certain mysql table, to a CSV file in a directory. The script is working fine and the files are saved in the right directory, but u want, that every order, exports to a separate CSV file.
Like:
12-09-2016-05-21_csvexport_1.CSV < data from order 1
12-09-2016-05-21_csvexport_2.CSV < data from order 2
And so on..
I want to run the script automatically with a cronjob. The orders to be exported as a separate csv file, can be selected with a date range. Like only create csv files from the last 2 days. It doesn't matter if the CSV files are going to overwrite.
The order table looks like this:
id_order date customer
1 01-01-2016 1223
2 01-02-2016 1224
3 01-03-2016 1225
4 01-04-2016 1226
5 01-05-2016 1227
And the php script looks like this:
<?php
error_reporting(E_ALL ^ E_DEPRECATED);
$host = 'localhost';
$user = 'user';
$pass = 'pass';
$db = 'database';
$table = 'columns';
$file = 'csvexport';
$dir = 'csv';
$link = mysql_connect($host, $user, $pass) or die("Can not connect." . mysql_error());
mysql_select_db($db) or die("Can not connect.");
function escape_csv_value($value) {
$value = str_replace('"', '""', $value);
if ( preg_match( '/\\r|\\n|,|"/', $value ) ) {
return '"' . str_replace( '"', '""', $value ) . '"';
} else {
return $value;
}
}
$result = mysql_query("SHOW COLUMNS FROM ".$table."");
$i = 0;
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result)) {
$csv_output .= $row['Field']."; ";
$i++;
}
}
$values = mysql_query("SELECT id_order, date, customer FROM orders");
while ($rowr = mysql_fetch_row($values)) {
for ($j=0;$j<$i;$j++) {
$csv_output .= escape_csv_value($rowr[$j]).';';
}
$csv_output .= "\n";
}
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header( "Content-disposition: attachment; filename=".$filename.".csv");
$filename = date("d-m-Y-i-s_", time()) . $file.".csv";
print $csv_output;
file_put_contents($dir ."/".$filename, $csv_output);
}
?>
I hope someone can help me. Thanks in advance.

Not able to apply formula to the excel spreadsheet using phpexcel?

I am using PHPExcel in my php code to generate an excel report with a number to spreadsheets but i want to apply formula complete columns in those spreadsheets and i am using the following code but its just applying the same values to the complete column but i want the formula to run and give values depending on the each cell column G of my worksheet, and i want these resulting values in column Q.
Following is the complete code that i am using:
<?php
$dbhost= "localhost"; //your MySQL Server
$dbuser = "root"; //your MySQL User Name
$dbpass = "root"; //your MySQL Password
$dbname = "pyramid";
//your MySQL Database Name of which database to use this
$tablename = "peachtree"; //your MySQL Table Name which one you have to create excel file
// your mysql query here , we can edit this for your requirement
$tablename1 = "shipping";
$tablename2 = "embassies";
$tablename3 = "documentprocesses";
$tablename4 = "documentdetails";
$tablename5 = "documents";
$sql = "Select * from $tablename ";
$sql1= "Select * from $tablename1";
$sql2= "Select * from $tablename2";
$sql3= "Select filter1.docID,
filter1.salesOrderID,
IF(filter1.docSource='shipping', 'Shipping', 'Non-Shipping') AS 'Doc Type',
filter1.countryID,
DATE(filter1.docTimeStamp) AS 'Date Received',
IF(filter2.processStartDate IS NULL, 'OPEN',
DATE(filter2.processStartDate)) AS 'Date Shipped',
$tablename3.processCode,
$tablename3.destination,
$tablename3.processStartDate,
$tablename4.*,
filter1.docValue
FROM ( SELECT docID,
salesOrderID,
docSource,
docValue,
countryID,
docTimeStamp
FROM $tablename5
WHERE DATE(docTimeStamp) <= '2015-03-31' ) AS filter1
JOIN (
SELECT docID,
processStartDate
FROM $tablename3
WHERE processCode = 'SHPD'
AND( processStatus <> 'completed'
OR DATE(processStartDate) > '2015-03-31')
) AS filter2
ON filter1.docID = filter2.docID
LEFT JOIN (
SELECT *
FROM $tablename3
WHERE DATE(processStartDate) <= '2015-03-31'
AND processStartDate IS NOT NULL) AS $tablename3
ON $tablename3.docID = filter2.docID
LEFT JOIN $tablename4 ON $tablename4.docID = filter2.docID";
//create code for connecting to mysql
$Connect = #mysql_connect($dbhost, $dbuser, $dbpass)
or die("Couldn't connect to MySQL:<br>" . mysql_error() . "<br>" . mysql_errno());
//select database
$Db = #mysql_select_db($dbname, $Connect)
or die("Couldn't select database:<br>" . mysql_error(). "<br>" . mysql_errno());
//execute query
$result = #mysql_query($sql,$Connect)
or die("Couldn't execute query:<br>" . mysql_error(). "<br>" . mysql_errno());
$result1 = #mysql_query($sql1,$Connect)
or die("Couldn't execute query:<br>" . mysql_error(). "<br>" . mysql_errno());
$result2 = #mysql_query($sql2,$Connect)
or die("Couldn't execute query:<br>" . mysql_error(). "<br>" . mysql_errno());
$result3 = #mysql_query($sql3,$Connect)
or die("Couldn't execute query:<br>" . mysql_error(). "<br>" . mysql_errno());
error_reporting(E_ALL);
set_time_limit(0);
ini_set('memory_limit','2500M');
require_once dirname(__FILE__) . '/Classes/PHPExcel.php';
$objPHPExcel = new PHPExcel();
// Set the active Excel worksheet to sheet 0
$objPHPExcel->setActiveSheetIndex(0);
// Initialise the Excel row number
$rowCount = 1;
//start of printing column names as names of MySQL fields
$column = 'A';
for ($i = 0; $i < mysql_num_fields($result); $i++)
{
$objPHPExcel->getActiveSheet()->setCellValue($column.$rowCount, mysql_field_name($result,$i));
$column++;
}
//end of adding column names
//start while loop to get data
$rowCount = 2;
while($row = mysql_fetch_row($result))
{
$column = 'A';
for($j=0; $j<mysql_num_fields($result);$j++)
{
if(!isset($row[$j]))
$value = NULL;
elseif ($row[$j] != "")
$value = strip_tags($row[$j]);
else
$value = "";
$objPHPExcel->getActiveSheet()->setCellValue($column.$rowCount, $value);
$column++;
}
$rowCount++;
}
$objPHPExcel->getActiveSheet()->setTitle('peachtree');
$objPHPExcel->createSheet();
$objPHPExcel->setActiveSheetIndex(1);
$rowCount = 1;
//start of printing column names as names of MySQL fields
$column = 'A';
for ($i = 0; $i < mysql_num_fields($result1); $i++)
{
$objPHPExcel->getActiveSheet()->setCellValue($column.$rowCount, mysql_field_name($result1,$i));
$column++;
}
//end of adding column names
//start while loop to get data
$rowCount = 2;
while($row = mysql_fetch_row($result1))
{
$column = 'A';
for($j=0; $j<mysql_num_fields($result1);$j++)
{
if(!isset($row[$j]))
$value = NULL;
elseif ($row[$j] != "")
$value = strip_tags($row[$j]);
else
$value = "";
$objPHPExcel->getActiveSheet()->setCellValue($column.$rowCount, $value);
$column++;
}
$rowCount++;
}
$objPHPExcel->getActiveSheet()->setTitle('shipping');
$objPHPExcel->createSheet();
$objPHPExcel->setActiveSheetIndex(2);
$rowCount = 1;
//start of printing column names as names of MySQL fields
$column = 'A';
for ($i = 0; $i < mysql_num_fields($result2); $i++)
{
$objPHPExcel->getActiveSheet()->setCellValue($column.$rowCount, mysql_field_name($result2,$i));
$column++;
}
//end of adding column names
//start while loop to get data
$rowCount = 2;
while($row = mysql_fetch_row($result2))
{
$column = 'A';
for($j=0; $j<mysql_num_fields($result2);$j++)
{
if(!isset($row[$j]))
$value = NULL;
elseif ($row[$j] != "")
$value = strip_tags($row[$j]);
else
$value = "";
$objPHPExcel->getActiveSheet()->setCellValue($column.$rowCount, $value);
$column++;
}
$rowCount++;
}
$objPHPExcel->getActiveSheet()->setTitle('embassies');
$objPHPExcel->createSheet();
$objPHPExcel->setActiveSheetIndex(3);
$rowCount = 1;
//start of printing column names as names of MySQL fields
$column = 'A';
for ($i = 0; $i < mysql_num_fields($result3); $i++)
{
$objPHPExcel->getActiveSheet()->setCellValue($column.$rowCount, mysql_field_name($result3,$i));
$column++;
}
$column = 'Q';
$objPHPExcel->getActiveSheet()->setCellValue($column.$rowCount,"SOS");
//end of adding column names
//start while loop to get data
$rowCount = 2;
while($row = mysql_fetch_row($result3))
{
$column = 'A';
for($j=0; $j<mysql_num_fields($result3);$j++)
{
if(!isset($row[$j]))
$value = NULL;
elseif ($row[$j] != "")
$value = strip_tags($row[$j]);
else
$value = "";
$objPHPExcel->getActiveSheet()->setCellValue($column.$rowCount, $value);
$column++;
}
***$column = 'Q';
$objPHPExcel->getActiveSheet()->setCellValue($column.$rowCount,'=IF(column="SOS",15,0)');
$rowCount++;***
}
$objPHPExcel->getActiveSheet()->setTitle('documentdata');
// Redirect output to a client’s web browser (Excel5)
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="results.xls"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
Wading through your wall of code
=IF(column="SOS",15,0)
Doesn't look like a valid Excel formula to me. What is column? If it was an Excel named range, or a cell reference, then it would work; but as it stands it's nothing that would be valid in an Excel formula.
Your formula must be a valid Excel formula
EDIT
If you want to set a single cell to a value of 15 or 0 depending on whether any cell in column G contains the value "SOS", then an appropriate MS Excel formula for that would be
=IF(ISERROR(MATCH("SOS",G:G, 0)), 0, 15)
If you want to do this check on a row by row basis, then you'd set your formula to something like:
$objPHPExcel->getActiveSheet()
->setCellValue($column.$rowCount,'=IF(G'.$rowCount.'="SOS",15,0)');
EDIT #2
As a way of debugging, for row #2 (ie cell Q2) that formula should be
=IF(G2="SOS",15,0)
for row #3 (cell Q3) it should be
=IF(G3="SOS",15,0)
etc
Do some basic debugging to be sure that your PHP is concatenating the formula value correctly

Categories