Jump to content

mysql to csv(or similar)


truck7758

Recommended Posts

<?php
mysql_select_db($database_CDBG, $CDBG);
$query_rsReport = "Your query";
$rsReport = mysql_query($query_rsReport, $CDBG) or die(mysql_error());
$totalRows_rsReport = mysql_num_rows($rsReport);
$fields = mysql_num_fields($rsReport);
for ($i = 0; $i < $fields; $i++)
{
$header .= mysql_field_name($rsReport, $i) . ",";
}
while ($row = mysql_fetch_row($rsReport))
{
$line = '';
foreach ($row as $value)
{
	$value = '"' . $value . '"' . ",";
	$line .= $value;
}
$data .= trim($line) . "\n";
}
$title = "Will appear in the top left cell";
$data = str_replace("\r", " ", $data);
header("Content-type: application/x-msdownload");
header("Content-Disposition: attachment; filename=cdbg_r1.csv");
header("Pragma: no-cache");
header("Expires: 0");
print "$title\n\n$header\n$data";
mysql_free_result($rsReport);
?>

 

This exports to csv...

 

I left most of my specific information in there to help make it a bit clearer what it is doing, hope that helps.

change the mysql_ items to mysqli items... should not be any difference once you do that if I remember (have only used mysqli once)

 

And CDBG is community development block grant... it is a specific to the script I wrote. You said you were a noob, I just did not want to cut out everything that made it sensible to look at

ok, heres what iv managed to get

 

<?php
$mysqli = new mysqli('localhost','username','password');
	$mysqli->select_db('orders');

$query_rsReport = "select * from orders";
$rsReport = mysqli->query($query_rsReport, $mysqli);
$totalRows_rsReport = mysqli->num_rows($rsReport);
$fields = mysqli->field_count($rsReport);
for ($i = 0; $i < $fields; $i++)
{
$header .= mysqli->field_name($rsReport, $i) . ",";
}
while ($row = mysqli->fetch_row($rsReport))
{
$line = '';
foreach ($row as $value)
{
	$value = '"' . $value . '"' . ",";
	$line .= $value;
}
$data .= trim($line) . "\n";
}
$title = "Will appear in the top left cell";
$data = str_replace("\r", " ", $data);
header("Content-type: application/x-msdownload");
header("Content-Disposition: attachment; filename=cdbg_r1.csv");
header("Pragma: no-cache");
header("Expires: 0");
print "$title\n\n$header\n$data";
$rsReport->close;
?>

 

this gives the error message:

 

Parse error: syntax error, unexpected T_OBJECT_OPERATOR in c:\webs\test\export.php on line 6

 

line 6 = $rsReport = mysqli->query($query_rsReport, $mysqli);

 

any ideas?

Thanks  ;D

iv now sorted that by making it: $rsReport = $mysqli->query($query_rsReport, $mysqli);

 

now im getting: Fatal error: Call to undefined method mysqli::num_rows() in c:\webs\test\export.php on line 7

 

line 7 = $totalRows_rsReport = $mysqli->num_rows($rsReport);

 

any ideas,

cheers  ;D

iv now managed to get to this

 

<?php
$mysqli = new mysqli('localhost','root','newr00t');
	$mysqli->select_db('orders');

$query_rsReport = "select * from orders";
$rsReport = $mysqli->query("SELECT * FROM orders");
$totalRows_rsReport = mysqli_num_rows($rsReport);
if (mysqli_field_count($rsReport)) {
    /* this was a select/show or describe query */
    $fields = mysqli_store_result($rsReport);

//$fields = mysqli_field_count($rsReport);
for ($i = 0; $i < $fields; $i++)
{
$header .= $mysqli->field_name($rsReport, $i) . ",";
}
while ($row = $mysqli->fetch_row($rsReport))
{
$line = '';
foreach ($row as $value)
{
	$value = '"' . $value . '"' . ",";
	$line .= $value;
}
$data .= trim($line) . "\n";
}
$title = "Will appear in the top left cell";
$data = str_replace("\r", " ", $data);
header("Content-type: application/x-msdownload");
header("Content-Disposition: attachment; filename=cdbg_r1.csv");
header("Pragma: no-cache");
header("Expires: 0");
print "$title\n\n$header\n$data";
mysqli_free_result($fields);
$rsReport->close;
?>

 

and now im getting the error: Parse error: syntax error, unexpected $end in c:\webs\test\export.php on line 36 which is my last line.

 

Any ideas,

Mike

sorted now. dont know how lol heres the code i used for anyone else who may find it useful

 

<?php
$host = 'localhost';
$user = 'username';
$pass = 'password';
$db = 'orders';
$table = 'orders';
$file = 'order';

$link = new mysqli(localhost, username, password) or die("Can not connect1." . mysql_error());
mysqli_select_db($link, "orders") or die("Can not connect2.");

$result = mysqli_query($link, "SHOW COLUMNS FROM ".$table."", MYSQLI_STORE_RESULT);
$i = 0;
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$csv_output .= $row['Field'].", ";
$i++;
}
}
$csv_output .= "\n";

$values = mysqli_query($link, "SELECT * FROM ".$table."");
while ($rowr = mysqli_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;
?>

 

Thanks All  ;D

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.