Exporting a CSV file from Php with query from Database - php

I am using following code to Export a CSV file
I am having very strange Problem.
My $newQuery gives me (when i do echo)
SELECT * FROM quotes WHERE storeEmail = 'xxxxxxx ';
If I use this above query in my phpmyadmin, i get atleast 1000 rows. However in this same code, its returning null.
$quotes is just empty.
what is wrong here
<?php session_start();
require_once("common/commonfiles.php");
$menuItem="stores";
$title="Stores";
if (isset($_GET['name']))
{
$name = trim($_GET['name']);
}
$filename = "export.csv";
$delimiter= ",";
// open raw memory as file so no temp files needed, you might run out of memory though
$f = fopen('php://memory', 'w');
// loop over the (input array
fputcsv($f, array("Call Details"), "\r\n\r\n");
fputcsv($f, array("UserId","Call Date","Firstname","Lastname","Phone","Zip","Email"), ",");
$query = "SELECT callDetails.* , clients.* FROM callDetails JOIN clients ON clients.id = callDetails.userId WHERE storeName = '" . $name ."' ";
db::open();
$result = mysql_query($query) or die(mysql_error());
$records = Array();
if(!mysql_num_rows($result)==0)
{
$i=0;
while ($row = mysql_fetch_array($result))
{
$records[$i] = $row;
$i++;
}
}
else
{
$records = false;
}
db::close();
if($records)
{
foreach ($records as $line)
{
$newArray = array($line['userId'],
$line['creationDate'],
$line['firstname'],
$line['lastname'],
$line['phone'],
$line['zip'],
$line['email']
);
// generate csv lines from the inner arrays
fputcsv($f, $newArray, $delimiter);
}
}
$query = "SELECT * FROM stores WHERE name LIKE '%".$name."%' LIMIT 1";
db::open();
$result = mysql_query($query) or die(mysql_error());
if(!mysql_num_rows($result)==0){
$recordset = mysql_fetch_assoc($result);
}else{
$recordset = false;
}
db::close();
$recordset = db::getRecord($query);
$storeEmail = $recordset['email'];
fputcsv($f, array(""), "\r\n\r\n");
fputcsv($f, array(""), "\r\n\r\n");
fputcsv($f, array(""), "\r\n\r\n");
fputcsv($f, array("Quote Details"), "\r\n\r\n");
fputcsv($f, array("Ring","Quote Time","Firstname","Lastname","Phone","Zip","Email"), ",");
$newQuery = "SELECT * FROM quotes WHERE storeEmail = '" . $storeEmail ."'";
db::open();
$newResult = mysql_query($newQuery) or die(mysql_error());
if(!mysql_num_rows($newResult)==0)
{
$i=0;
while ($row = mysql_fetch_array($newResult))
{
$quotes[$i] = $row;
$i++;
}
}
else
{
$quotes = false;
}
db::close();
if($quotes)
{
foreach ($quotes as $line)
{
$newArray = array($line['ring'],
$line['quoteTime'],
$line['firstname'],
$line['lastname'],
$line['phone'],
$line['zip'],
$line['email']
);
// generate csv lines from the inner arrays
fputcsv($f, $newArray, $delimiter);
}
}
// rewrind the "file" with the csv lines
fseek($f, 0);
// tell the browser it's going to be a csv file
header('Content-Type: application/csv');
// tell the browser we want to save it instead of displaying it
header('Content-Disposition: attachement; filename="'.$filename.'";');
// make php send the generated csv lines to the browser
fpassthru($f);
//fclose($fp);
?>

Try to use trim in line:
$newQuery = "SELECT * FROM quotes WHERE storeEmail = '" . trim($storeEmail) ."'";
Maybe $storeEmail variable has some additional whitespace characters.

Are you actually making it into your mysql_num_rows loop? If not, then $quotes is being set to false. Try printing something out if you make it there or don't.
if(!mysql_num_rows($newResult)==0) {
print "Made it!";
}
else {
print "Didn't Make It. How Sad.";
}
Then you will know if it's your query or not.
If it is your query, then it is probably failing at the email part. ($storeEmail = $recordset['email'];) Try printing your query to see if that looks right:
$newQuery = "SELECT * FROM quotes WHERE storeEmail = '" . $storeEmail ."'";
print $newQuery;
If not there, then your query that gets the email is having problems.

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 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.

Export data from mysql into text file?

I am trying to export some data from a mysql table into a text file.
However, I only get a few , written into my text file without the actual data in the members table!
I am using the code bellow:
<?php
$fh = fopen('email-data.txt', 'w');
include "../config/connect.php";
/* insert field values into data.txt */
$sql = "SELECT * FROM members";
$query = mysqli_query($db_conx, $sql);
while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
$num = mysqli_num_fields($query) ;
$last = $num - 1;
for($i = 0; $i < $num; $i++) {
fwrite($fh, $row[$i]);
if ($i != $last) {
fwrite($fh, ",");
}
}
fwrite($fh, "\n");
}
fclose($fh);
?>
could someone please help me out with this?
Change MYSQLI_ASSOC to MYSQLI_NUM
You are fetching rows with Numbers But using an Assiciative array
something like this? you receive an assoc array, without numeric indices
<?php
$fh = fopen('email-data.txt', 'w');
include "../config/connect.php";
/* insert field values into data.txt */
$sql = "SELECT * FROM members";
$query = mysqli_query($db_conx, $sql);
while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
//$num = mysqli_num_fields($query) ;
//$last = $num - 1;
//for($i = 0; $i < $num; $i++) {
$last = sizeof($row)-1;
$i = 0;
foreach($row as $field) {
fwrite($fh, $field);
if ($i != $last) {
fwrite($fh, ",");
}
$i++;
}
fwrite($fh, "\n");
}
fclose($fh);
?>
Change the second argument of mysqli_fetch_array() function.
While loop should look like while($row = mysqli_fetch_array($query, MYSQLI_NUM)){...}

Convert strings into asterisks

im working on a php-based system. one of its features is allows user to download an excel file containing all the information in one of my table in my database. my problem is, one data of that information is classified to other users. thus, i want to convert the output of that data into a string of asterisk.
<?PHP
//MySQL Database Connect
include 'datalogin.php';
function cleanData(&$str)
{
$str = preg_replace("/\t/", "\\t", $str);
$str = preg_replace("/\r?\n/", "\\n", $str);
if(strstr($str, '"')) $str = '"' . str_replace('"', '""', $str) . '"';
}
# filename for download
$filename = "website_data_" . date('Ymd') . ".xls";
header("Content-Disposition: attachment; filename=\"$filename\"");
header("Content-Type: application/vnd.ms-excel");
$flag = false;
//$result = pg_query("SELECT * FROM data_mapping ORDER BY CE_Hostname") or die('Query failed!');
/*$query = "SELECT * FROM data_mapping";
while(false !== ($row = pg_fetch_assoc($result))) {
//while(false !== ($row = pg_fetch_assoc($result))) {
//$result=mysql_query($query);
$row=mysql_fetch_array($result);
$row=mysql_fetch_assoc ($result);
//$row=$row['Cust_Segment'];
// foreach($data as $row)
# display field/column names as first row
if(!$flag) {
echo implode("\t", array_keys($row)) . "\r\n";
$flag = true;
}
array_walk($row, 'cleanData');
echo implode("\t",($row)) . "\r\n";
}
*/
$sql = 'SELECT CE_Hostname, Cust_Segment, Cust_Site_Name, CE_WAN_IP_Addr, CE_Bkp_IP_Addr, Cust_Name, Svc_Type, com_string FROM data_mapping';
$result = mysql_query($sql);
if (!$result) {
echo "DB Error, could not query the database<br>";
echo 'MySQL Error: ' . mysql_error();
exit;
}
echo "Hostname\t Group/System\t Site Name\t IP ADDR\t BKP IP ADDR\t System Name\t Device Type\t Comm_String\r\n";
while ($row = mysql_fetch_assoc($result)) {
echo implode("\t",($row)) . "\r\n";
}
mysql_free_result($result);
//exit;
?>
i want to convert only the comm_string result. thanks.
$sql = 'SELECT CE_Hostname, Cust_Segment, Cust_Site_Name, CE_WAN_IP_Addr, CE_Bkp_IP_Addr, Cust_Name, Svc_Type, com_string FROM data_mapping';
Can simply be updated to not included the classified field
$sql = 'SELECT CE_Hostname, Cust_Segment, Cust_Site_Name, CE_WAN_IP_Addr, CE_Bkp_IP_Addr, Cust_Name, Svc_Type FROM data_mapping';
Or if you still want to show some value there without any change in your PHP code then you could do
$sql = 'SELECT CE_Hostname, Cust_Segment, Cust_Site_Name, CE_WAN_IP_Addr, CE_Bkp_IP_Addr, Cust_Name, Svc_Type, 'SECRET' FROM data_mapping';
This will then show SECRET instead of that code
Try this:
while ($row = mysql_fetch_assoc($result))
{
$row['Comm_String'] = preg_replace( '/./', '*', $row['Comm_String'] );
echo implode("\t",($row)) . "\r\n";
}

Search and erase db rows with file checker

Hi I am trying to get a file path from the embed field, use that path to see if a file exists in that path. If the file dose not exist then delete that entry. I am running a game site and the script that downloads the game file skipped a few so its like finding a needle in a 4000 entry db haystack. Any help is appreciated. Here is my code:
<?php
if(isset($_POST['clean'])) {
$query = "SELECT * FROM games WHERE embed";
$result = mysql_query($query) or die ("no query");
$result_array_path = array();
while($row = mysql_fetch_assoc($result))
{
$result_array_path = $row;
}
$count = mysql_num_rows($result);
for($counter=0;$counter<$count;$counter++){
if (file_exists($result_array_path[$counter])){
}else{
mysql_query("DELETE FROM games WHERE embed".$result_array_path[$counter]);
echo $result_array_path[$counter]." ";
}
}
}
?>
--------------EDIT-------------
I Updated the code to BUT it decides to delete my entire database instead of deleting the missing game entries. Here is the revised code:
<?php
if(isset($_POST['clean'])) {
$query = "SELECT * FROM games WHERE embed NOT LIKE '%mochiads.com%'";
$result = mysql_query($query) or die ("no query");
$result_array_path = array();
while($row = mysql_fetch_assoc($result))
{
$result_array_path[] = $row['embed'];
}
foreach($result_array_path as $path) {
if(!file_exists($path)) {
mysql_query("DELETE FROM games WHERE embed = '" . $path . "'");
echo $path." | ";
}
}
}
?>
-----------EDIT--------------
I debugged the program so it works now. Had to add a "$_SERVER['DOCUMENT_ROOT']" to the program. Here is the finished program
<?php
if(isset($_POST['clean'])) {
$query = "SELECT * FROM games WHERE embed NOT LIKE '%mochiads.com%'";
$result = mysql_query($query) or die ("no query");
$result_array_path = array();
while($row = mysql_fetch_assoc($result))
{
$result_array_path[] = $row['embed'];
}
foreach($result_array_path as $path) {
$rel_path = str_replace('http://www.flamegame.net', '', $path);
$rel_path = str_replace('http://flamegame.net', '', $rel_path);
$rel_path = str_replace('%20', ' ', $rel_path);
if(! file_exists($_SERVER['DOCUMENT_ROOT'] . $rel_path)) {
mysql_query("DELETE FROM games WHERE embed = '" . $path . "'");
echo $rel_path." | ";
}
}
}
?>
Thanks For all of the help Especially Gargron.
For those who are wondering this program is for my website:
http://www.FlameGame.net
I see one typo that might be your problem:
$result_array_path = array();
while($row = mysql_fetch_assoc($result))
{
// You want to append the row to the results array
// instead of replacing the array each time, so []
$result_array_path[] = $row['embed'];
// That's assuming the table field containing the path is "embed"
}
And instead of using mysql_num_rows you could instead iterate through the items in $result_array_path like this:
foreach($result_array_path as $path) {
if(! file_exists($path)) {
// Delete
// You missed a = in the query too
mysql_query("DELETE FROM games WHERE embed = '" . $path . "'");
}
}
That should make it work.

Categories