I am switching hosts and copying the files over without change. I get the following error when trying to create a spreadsheet:

Fatal error: Call to a member function on a non-object in
/home/pmiweb/www/www/download_stores.php on line 68

Here is the code from the page:

<?php
include 'includes/db.php';
require_once 'Spreadsheet/Excel/Writer.php';

$query = sprintf("SELECT * FROM store_list WHERE sl_warehouse_num = '%s' ORDER BY sl_customer, sl_state", mysql_real_escape_string($user_warehouse_num));
$result = mysql_query($query);

if (!$result) {
   die('invalid query: ' . mysql_error());
}

$num_rows = mysql_num_rows($result);

if($num_rows > 0){

   $row_count = 1;

   while($row = mysql_fetch_array($result)){

        if($row_count == 1){

        // Creating a workbook
        $workbook = new Spreadsheet_Excel_Writer();
        $format_bold =& $workbook->addFormat();
        $format_bold->setBold();

        // sending HTTP headers
        $workbook_name = $row['sl_warehouse'] . '-Stores.xls';
        $workbook->send($workbook_name);

        // Creating a worksheet
        $worksheet =& $workbook->addWorksheet('Store List');

        // The actual data
        $worksheet->setColumn(0,0,20);
        $worksheet->setColumn(1,1,13);
        $worksheet->setColumn(2,2,30);
        $worksheet->setColumn(3,3,13);
        $worksheet->setColumn(4,4,45);
        $worksheet->setColumn(5,5,13);
        $worksheet->setColumn(6,6,13);
        $worksheet->setColumn(7,7,13);
        $worksheet->setColumn(8,8,13);
        $worksheet->write(0, 0, 'Distributor', $format_bold);
        $worksheet->write(0, 1, 'Customer', $format_bold);
        $worksheet->write(0, 2, 'Ship To Name', $format_bold);
        $worksheet->write(0, 3, 'Store', $format_bold);
        $worksheet->write(0, 4, 'Address', $format_bold);
        $worksheet->write(0, 5, 'City', $format_bold);
        $worksheet->write(0, 6, 'State', $format_bold);
        $worksheet->write(0, 7, 'Zip', $format_bold);
        $worksheet->write(0, 8, 'Phone', $format_bold);
        }

        $worksheet->write($row_count, 0, $row['sl_warehouse']);
        $worksheet->write($row_count, 1, $row['sl_customer']);
        $worksheet->write($row_count, 2, $row['sl_name']);
        $worksheet->write($row_count, 3, $row['sl_store']);
        $worksheet->write($row_count, 4, $row['sl_address']);
        $worksheet->write($row_count, 5, $row['sl_city']);
        $worksheet->write($row_count, 6, $row['sl_state']);
        $worksheet->write($row_count, 7, $row['sl_zip']);
        $worksheet->write($row_count, 8, $row['sl_phone']);
        $row_count++;
    }
}
// Let's send the file
$workbook->close(); // <--------- Line 68 ------------
?>

$workbook->close(); //Line 68

This file worked fine on the other server. Pear and Spreadsheet_Excel_Writer are installed.

Any ideas?

Travis

    $workbook is only set if $num_rows > 0. If $num_rows<=0, then $workbook isn't set and therefore doesn't contain an object. So... what is the value of $num_rows?

    Incidentally, All the stuff in here:

                if($row_count == 1){...} 

    Why not just do it before starting the loop? (I.e., do all the $workbook = new Spreadsheet....and column headings in the place where you set $row_count=1). And put $workbook->close() immediately after the loop so that you only attempt to close it if it actually exists.

      Thanks Weedpacket. That made me realize that no data was being returned from the table and that was because I left out the session_start();.

      <?php
      session_start(); //added this to get data from table
      include 'includes/db.php';
      require_once 'Spreadsheet/Excel/Writer.php';
      

      Now that I have added the session_start I get some new errors:

      Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/pmiweb/www/www/download_stores.php:3) in /home/pmiweb/www/www/download_stores.php on line 4
      
      Warning: Cannot modify header information - headers already sent by (output started at /home/pmiweb/www/www/download_stores.php:3) in /home/pmiweb/www/www/Spreadsheet/Excel/Writer.php on line 68
      
      Warning: Cannot modify header information - headers already sent by (output started at /home/pmiweb/www/www/download_stores.php:3) in /home/pmiweb/www/www/Spreadsheet/Excel/Writer.php on line 69
      
      Warning: Cannot modify header information - headers already sent by (output started at /home/pmiweb/www/www/download_stores.php:3) in /home/pmiweb/www/www/Spreadsheet/Excel/Writer.php on line 70
      
      Warning: Cannot modify header information - headers already sent by (output started at /home/pmiweb/www/www/download_stores.php:3) in /home/pmiweb/www/www/Spreadsheet/Excel/Writer.php on line 71
      
      Warning: Cannot modify header information - headers already sent by (output started at /home/pmiweb/www/www/download_stores.php:3) in /home/pmiweb/www/www/Spreadsheet/Excel/Writer.php on line 72

      How can I include the session data without getting the header errors with the spreadsheet_excel_writer?

        I figured it out. There was a space before the <?php at the top of my file.

          Write a Reply...