View unanswered posts | View active topics It is currently Sat Jun 25, 2022 3:33 am

Reply to topic  [ 1 post ] 
 sql to excel 
Author Message
Junior Member

Joined: Wed Nov 11, 2009 8:31 pm
Posts: 26
Post sql to excel

I am trying to save my sql results into an excel file. I managed to found a script from google search. It works perfectly well if my results are all allocate in one single table. However, i am using joined-table to show my results.

Can anyone guide me along how am i suppose to solve this problem?
Million thanks to you...

this is the original script:
[PHP]while($row = mysql_fetch_row($results)){
$line = '';
foreach($row as $value){
if(!isset($value) || $value == ""){
$value = "\t";
# important to escape any quotes to preserve them in the data.
$value = str_replace('"', '""', $value);
# needed to encapsulate data in quotes because some data might be multi line.
# the good news is that numbers remain numbers in Excel even though quoted.
$value = '"' . $value . '"' . "\t";
$line .= $value;
$data .= trim($line)."\n";
} $data = str_replace("\r", "", $data);

# Nice to let someone know that the search came up empty.
# Otherwise only the column name headers will be output to Excel.
if ($data == "") {
$data = "\nno matching records found\n";
# This line will stream the file to the user rather than spray it across the screen
header("Content-type: application/octet-stream");
# replace excelfile.xls with whatever you want the filename to default to
header("Content-Disposition: attachment; filename=excelfile.xls");
header("Pragma: no-cache");
header("Expires: 0");

this is mine using join table:
[PHP]while ($row = mysql_fetch_array($results, MYSQL_ASSOC))
$device_num = $row['device_num'];
$device_rev = $row['device_rev'];
$device_site = $row['device_site'];
$device_platform = $row['device_platform'];
$device_dibid = $row['device_dibid'];
$device_designid = $row['device_designid'];
$socketnum = $row['socket_idnum'];
$fedexnum = $row['fedex_num'];
$subcon_name = $row['subcon_name'];
//$subconsite = $row['subcon_site'];
$device_socketID = $row['device_socketID'];
$device_subconID = $row['device_subconID'];
$device_tpe = $row['device_tpe'];
$device_remark = $row['device_remark'];
$date = $row['date'];

/* for this part, i retrieve the result from the respective table into the main table */
$query_rev = "SELECT rev_label
FROM rev
WHERE rev_id = '$device_rev' ";
$results_rev = mysql_query($query_rev)
or die (mysql_error());
$row_rev = mysql_fetch_array ($results_rev);
//extract ($row_rev);
$rev_label = $row_rev['rev_label'];

/* for this part, the retrieved result from the respective table into the main table */
<td bgcolor="#ffffff" width="10%">
echo $device_num ;
<td bgcolor="#ffffff" width="2%">
echo $rev_label ;

Tue Dec 29, 2009 2:46 am
Report this post
Display posts from previous:  Sort by  
Reply to topic   [ 1 post ] 

Who is online

Users browsing this forum: No registered users and 1 guest

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
© Copyright 2003-2008 The ultimate PHP Editor and PHP IDE site.