Jump to content

Combine two tables from MYSQL into a single csv file


goldenbrown21

Recommended Posts

Hey all!

 

I've been stuck getting my exporting php script to display the results correctly.

At the moment, when I use the export sheet, it only displays the results for table 1  ???, whereas I want it to display table 1 and table 2.

 

See my script below:

<?php
$host = 'localhost';
$user = 'root';
$pass = '******';
$db = 'nsc';
$table1 = 'customerdetails';
$table2 = 'callout';
$file = 'export';

$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 ".$table1."");
$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 ".$table1.",".$table2." WHERE ".$table1.".custcode = ".$table2.".custcode ORDER BY ".$table1.".custcode ASC");
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;
?>

 

Any assistance with this would be greatly appreciated!!!

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.