/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Creating a csv file

does anyone know how to create a csv file from a single record in the database table?

my code currently will only dump the entire contents of the table to the file. if i put a WHERE statement in there attached to a session variable it tells me the variable is not declared and that the sql statement is not a valid one.

i want to be able to type into a text box and click the link to download the csv file for that specific information.

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@neekworldauthorJul 03.2009 — here is the code im using for the csv.php file

<?php

session_start();

$host = '';

$user = '';

$pass = '';

$db = '';

$table = '';

$file = 'export';

$csv_output = '';

$link = mysql_connect($host, $user, $pass) or die("Can not connect." . mysql_error());

mysql_select_db($db) or die("Can not connect.");

$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++;

}

}

$csv_output .= "n";

$values = mysql_query("SELECT * FROM ".$table ." WHERE oderID =" . $_GET['orderID']."");

while ($rowr = mysql_fetch_row($values)) {

for ($j=0;$j<$i;$j++) {

$csv_output .= '" '.$rowr[$j].' ",';

}

$csv_output .= "n";

}

$filename = $file."_".date("Y-m-d_H-i",time());

header("Content-type: application/vnd.ms-excel");

header("Content-disposition: csv" . date("Y-m-d") . ".csv");

header( "Content-disposition: filename=".$filename.".csv");

print $csv_output;

exit;

?>
Copy linkTweet thisAlerts:
@neekworldauthorJul 03.2009 — ok i fixed it myself. if i use a button in the form it submits the post variable to the csv.php script and it inserts only the record that i want in the excel file. yeah!!!!!

code here if anyone else would like to know how to do it.

this code will pop up a dialog box for you to save the file where you want. it has an auto name that uses the date/time stamp to name the file but i guess you could change that to what ever when the dialog box pops up.

this is the code from the main page.
<i>
</i> &lt;form id="form1" name="form1" method="post" action="mycsv.php"&gt;
&lt;label&gt;Order ID&lt;br /&gt;
&lt;input type="text" name="orderID" id="orderID" /&gt;
&lt;/label&gt;
&lt;input name="Submit" type="submit" /&gt;
&lt;/form&gt;
&lt;?php
$orderID = $_POST['orderID'];
$_SESSION['orderID'] = $orderID;
?&gt;


mycsv.php file
&lt;?php

$host = 'insert your host here'; // MYSQL database host adress
$db = 'insert your database here'; // MYSQL database name
$user = 'insert your database user here'; // Mysql Datbase user
$pass = 'insert database password'; // Mysql Datbase password

// Connect to the database
$link = mysql_connect($host, $user, $pass);
mysql_select_db($db);

require 'exportcsv.inc.php';

$table="insert your table here"; // this is the tablename that you want to export to csv from mysql.

exportMysqlToCsv($table);

?&gt;


exportcsv.inc.php file
<i>
</i>&lt;?php

function exportMysqlToCsv($table,$filename = 'export.csv')
{
$csv_terminated = "n";
$csv_separator = ",";
$csv_enclosed = '"';
$csv_escaped = "\";
$sql_query = "select * from $table WHERE orderID =" .$_POST['orderID']."";

<i> </i>// Gets the data from the database
<i> </i>$result = mysql_query($sql_query);
<i> </i>$fields_cnt = mysql_num_fields($result);


<i> </i>$schema_insert = '';

<i> </i>for ($i = 0; $i &lt; $fields_cnt; $i++)
<i> </i>{
<i> </i> $l = $csv_enclosed . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed,
<i> </i> stripslashes(mysql_field_name($result, $i))) . $csv_enclosed;
<i> </i> $schema_insert .= $l;
<i> </i> $schema_insert .= $csv_separator;
<i> </i>} // end for

<i> </i>$out = trim(substr($schema_insert, 0, -1));
<i> </i>$out .= $csv_terminated;

<i> </i>// Format the data
<i> </i>while ($row = mysql_fetch_array($result))
<i> </i>{
<i> </i> $schema_insert = '';
<i> </i> for ($j = 0; $j &lt; $fields_cnt; $j++)
<i> </i> {
<i> </i> if ($row[$j] == '0' || $row[$j] != '')
<i> </i> {

<i> </i> if ($csv_enclosed == '')
<i> </i> {
<i> </i> $schema_insert .= $row[$j];
<i> </i> } else
<i> </i> {
<i> </i> $schema_insert .= $csv_enclosed .
<i> </i> str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, $row[$j]) . $csv_enclosed;
<i> </i> }
<i> </i> } else
<i> </i> {
<i> </i> $schema_insert .= '';
<i> </i> }

<i> </i> if ($j &lt; $fields_cnt - 1)
<i> </i> {
<i> </i> $schema_insert .= $csv_separator;
<i> </i> }
<i> </i> } // end for

<i> </i> $out .= $schema_insert;
<i> </i> $out .= $csv_terminated;
<i> </i>} // end while

<i> </i>header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
<i> </i>header("Content-Length: " . strlen($out));
<i> </i>// Output to browser with appropriate mime type, you choose;)
<i> </i>header("Content-type: text/x-csv");
<i> </i>//header("Content-type: text/csv");
<i> </i>//header("Content-type: application/csv");
<i> </i>$filename = $filename."_".date("Y-m-d_H-i",time());
<i> </i>header("Content-disposition: csv" . date("Y-m-d") . ".csv");
<i> </i>header( "Content-disposition: filename=".$filename.".csv");

<i> </i>//header("Content-Disposition: attachment; filename=$filename");
<i> </i>echo $out;
<i> </i>exit;

}

?&gt;


if you want it to export a single row this will work just change the line below to your variable

$sql_query = "select * from $table WHERE orderID =" .$_POST['orderID']."";


or if you want it to download the entire contents of the table change it to

$sql_query = "select * from $table";
×

Success!

Help @neekworld spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...