Page 1 of 1

MySQL to Excel Problems (fixed)

Posted: Sat Feb 21, 2004 1:14 pm
by efriese
I'm trying to export a mysql data base to an excel file. I got the following code off star geek:

Code: Select all

<?php
//Written by Dan Zarrella. Some additional tweaks provided by JP Honeywell
//pear excel package has support for fonts and formulas etc.. more complicated
//this is good for quick table dumps (deliverables)

include('DB_connection.php');
$result = mysql_query('select * from excel_test', $linkID);
$count = mysql_num_fields($result);

for ($i = 0; $i < $count; $i++)&#123;
    $header .= mysql_field_name($result, $i)."\t";
&#125;

while($row = mysql_fetch_row($result))&#123;
  $line = '';
  foreach($row as $value)&#123;
    if(!isset($value) || $value == "")&#123;
      $value = "\t";
    &#125;else&#123;
# 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";
    &#125;
    $line .= $value;
  &#125;
  $data .= trim($line)."\n";
&#125;
# this line is needed because returns embedded in the data have "\r"
# and this looks like a "box character" in Excel
  $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 == "") &#123;
  $data = "\nno matching records found\n";
&#125;

# 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");

echo $header."\n".$data; 
?>
I'm getting some errors and I am new to PHP, so I could use some help understanding them. Here's a printout of the errors:
Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /home/home8/friesef/public_html/excel2.php on line 12

Warning: mysql_num_fields(): supplied argument is not a valid MySQL result resource in /home/home8/friesef/public_html/excel2.php on line 13

Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in /home/home8/friesef/public_html/excel2.php on line 19

Warning: Cannot modify header information - headers already sent by (output started at /home/home8/friesef/public_html/excel2.php:12) in /home/home8/friesef/public_html/excel2.php on line 47

Warning: Cannot modify header information - headers already sent by (output started at /home/home8/friesef/public_html/excel2.php:12) in /home/home8/friesef/public_html/excel2.php on line 50

Warning: Cannot modify header information - headers already sent by (output started at /home/home8/friesef/public_html/excel2.php:12) in /home/home8/friesef/public_html/excel2.php on line 51

Warning: Cannot modify header information - headers already sent by (output started at /home/home8/friesef/public_html/excel2.php:12) in /home/home8/friesef/public_html/excel2.php on line 52
no matching records found
Any help would be much appreciated!

Posted: Sat Feb 21, 2004 1:17 pm
by markl999
Looks like your mysql_connect() failed, check you have entered the correct mysql username and password.

Posted: Sat Feb 21, 2004 1:33 pm
by efriese
The username and password is right. Any other ideas?

Posted: Sat Feb 21, 2004 1:36 pm
by markl999
Wherever you have mysql_connect() or mysql_query() add a mysql_error(), eg.
$linkID = mysql_connect('localhost', 'user', 'pass') or die(mysql_error());

and ...

$result = mysql_query('select * from excel_test', $linkID) or die(mysql_error());

That should give you some clues as to what's going wrong.

Posted: Sat Feb 21, 2004 1:45 pm
by efriese
Found the error. I forgot to specify the database...thanks for the help!