Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8181235
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T00:27:05+00:00 2026-06-07T00:27:05+00:00

I am using the following code to generate excel from sql through php.this is

  • 0

I am using the following code to generate excel from sql through php.this is a modeifed verion of the working code ..

WORKING CODE (NO ERROR)

<?php
//documentation on the spreadsheet package is at:
//http://pear.php.net/manual/en/package.fileformats.spreadsheet-excel-writer.php
chdir('phpxls');
require_once 'Writer.php';
chdir('..');

$sheet1 =  array(
  array('eventid','eventtitle'       ,'datetime'           ,'description'      ,'notes'      ),
  array('5'      ,'Education Seminar','2010-05-12 08:00:00','Increase your WPM',''           ),
  array('6'      ,'Work Party'       ,'2010-05-13 15:30:00','Boss\'s Bday'     ,'bring tacos'),
  array('7'      ,'Conference Call'  ,'2010-05-14 11:00:00','access code x4321',''           ),
  array('8'      ,'Day Off'          ,'2010-05-15'         ,'Go to Home Depot' ,''           ),
);

$sheet2 =  array(
  array('eventid','funny_name'   ),
  array('32'      ,'Adam Baum'    ),
  array('33'      ,'Anne Teak'    ),
  array('34'      ,'Ali Katt'     ),
  array('35'      ,'Anita Bath'   ),  
  array('36'      ,'April Schauer'),
  array('37'      ,'Bill Board'   ),
);

$workbook = new Spreadsheet_Excel_Writer();

$format_und =& $workbook->addFormat();
$format_und->setBottom(2);//thick
$format_und->setBold();
$format_und->setColor('black');
$format_und->setFontFamily('Arial');
$format_und->setSize(8);

$format_reg =& $workbook->addFormat();
$format_reg->setColor('black');
$format_reg->setFontFamily('Arial');
$format_reg->setSize(8);

$arr = array(
      'Calendar'=>$sheet1,
      'Names'   =>$sheet2,
      );

foreach($arr as $wbname=>$rows)
{
    $rowcount = count($rows);
    $colcount = count($rows[0]);

    $worksheet =& $workbook->addWorksheet($wbname);

    $worksheet->setColumn(0,0, 6.14);//setColumn(startcol,endcol,float)
    $worksheet->setColumn(1,3,15.00);
    $worksheet->setColumn(4,4, 8.00);

    for( $j=0; $j<$rowcount; $j++ )
    {
        for($i=0; $i<$colcount;$i++)
        {
            $fmt  =& $format_reg;
            if ($j==0)
                $fmt =& $format_und;

            if (isset($rows[$j][$i]))
            {
                $data=$rows[$j][$i];
                $worksheet->write($j, $i, $data, $fmt);
            }
        }
    }
}

$workbook->send('test.xls');
$workbook->close();

//-----------------------------------------------------------------------------
?>

MODIFIED CODE TO RETRIEVE DATA FROM DATABASE (WHICH SHOWS ERROR)

<?php

    $numrow12 = mysql_query("SELECT * FROM mastertable WHERE pdfstatus = 1 ORDER BY tstamp DESC"); 

    mysql_select_db("brainoidultrafb", $link);
    chdir('phpxls');
    require_once 'Writer.php';
    chdir('..');

    while($row = mysql_fetch_array($numrow12)) {
        $sheet1 =  array(
      array('StudentID','Students Email' ,'Diagnosis','TimeStamp' ,),
      array($row['student_id'] ,$row['email_id'],$row['diagnosis'],$row['tstamp'],),
    );

    } 



    $workbook = new Spreadsheet_Excel_Writer();

    $format_und =& $workbook->addFormat();
    $format_und->setBottom(2);//thick
    $format_und->setBold();
    $format_und->setColor('black');
    $format_und->setFontFamily('Arial');
    $format_und->setSize(8);

    $format_reg =& $workbook->addFormat();
    $format_reg->setColor('black');
    $format_reg->setFontFamily('Arial');
    $format_reg->setSize(8);

    $arr = array(
          'Calendar'=>$sheet1,
              );

    foreach($arr as $wbname=>$rows)
    {
        $rowcount = count($rows);
        $colcount = count($rows[0]);

        $worksheet =& $workbook->addWorksheet($wbname);

        $worksheet->setColumn(0,0, 6.14);//setColumn(startcol,endcol,float)
        $worksheet->setColumn(1,3,15.00);
        $worksheet->setColumn(4,4, 8.00);

        for( $j=0; $j<$rowcount; $j++ )
        {
            for($i=0; $i<$colcount;$i++)
            {
                $fmt  =& $format_reg;
                if ($j==0)
                    $fmt =& $format_und;

                if (isset($rows[$j][$i]))
                {
                    $data=$rows[$j][$i];
                    $worksheet->write($j, $i, $data, $fmt);
                }
            }
        }
    }

    $workbook->send('Submissions.xls');
    $workbook->close();

    //-----------------------------------------------------------------------------
    ?>

Its showing the following errors

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /home/content/58/9508458/html/psychiatric/subexcel.php on line 13

Warning: Cannot modify header information – headers already sent by (output started at /home/content/58/9508458/html/psychiatric/subexcel.php:13) in /home/content/58/9508458/html/psychiatric/phpxls/Writer.php on line 67

Warning: Cannot modify header information – headers already sent by (output started at /home/content/58/9508458/html/psychiatric/subexcel.php:13) in /home/content/58/9508458/html/psychiatric/phpxls/Writer.php on line 68

Warning: Cannot modify header information – headers already sent by (output started at /home/content/58/9508458/html/psychiatric/subexcel.php:13) in /home/content/58/9508458/html/psychiatric/phpxls/Writer.php on line 69

Warning: Cannot modify header information – headers already sent by (output started at /home/content/58/9508458/html/psychiatric/subexcel.php:13) in /home/content/58/9508458/html/psychiatric/phpxls/Writer.php on line 70

Warning: Cannot modify header information – headers already sent by (output started at /home/content/58/9508458/html/psychiatric/subexcel.php:13) in /home/content/58/9508458/html/psychiatric/phpxls/Writer.php on line 71

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T00:27:08+00:00Added an answer on June 7, 2026 at 12:27 am

    Right now, you’re selecting the database after you try to query the database:

    $numrow12 = mysql_query("SELECT * FROM mastertable WHERE pdfstatus = 1 ORDER BY tstamp DESC");
    
    mysql_select_db("brainoidultrafb", $link);
    

    That won’t work. Just swap the order so you select the database, then query it:

    mysql_select_db("brainoidultrafb", $link);
    
    $numrow12 = mysql_query("SELECT * FROM mastertable WHERE pdfstatus = 1 ORDER BY tstamp DESC");
    

    As for your second problem, you’re overwriting $sheet1 on each iteration of the loop. Do this instead:

    $sheet1 = array(
        array('StudentID','Students Email' ,'Diagnosis','TimeStamp' ,)
    );
    
    while($row = mysql_fetch_array($numrow12)) {
        $sheet1[] = array($row['student_id'], $row['email_id'], $row['diagnosis'], $row['tstamp'],);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to PHP. I'm using the following code to generate an excel sheet
I'm using the following code to generate a random string: <?php function random_string( )
I'm trying to generate excel using c# following is the code snippet for it
I'm currently using the following code to generate an active state on links: <?php
I am currently using the following PHP code generate a Select Box with different
I am using the following code: PHP: // Generate Guid function NewGuid() { $s
In order to generate a XML i am using following Code currently. .<?php require
Hi I'm using the following code to generate a CSV file containing data from
I'm using the following code to generate an Excel file using Microsoft.Interop.Excel. The problem
Iam using following code to generate CAPTCHA : $captcha = $this->createElement('captcha', 'captcha', array('required' =>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.