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

  • Home
  • SEARCH
  • 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 3438446
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:11:20+00:00 2026-05-18T08:11:20+00:00

I have some PHP script for export MYSQL table into Excel format, but I

  • 0

I have some PHP script for export MYSQL table into Excel format, but I get some problem after the table have exported. The data which get from db just show only one row.

This the following script:

$dbc=mysql_connect(_SRV,_ACCID,_PWD) or die(_ERROR15.": ".mysql_error());
$db=mysql_select_db("qdbase",$dbc) or die(_ERROR17.": ".mysql_error());

$sQuery = "SELECT id, Line, Model,Lot_no,
                  COUNT( Serial_number ) AS Qty,
                  SUM(S), SUM(A), SUM(B), SUM(C),
                  (SUM(S) + SUM(A) + SUM(B)*0.4 + SUM(C)*0.1) / COUNT(Serial_number) AS QP,
                  ROUND((SUM(S) + SUM(A) + SUM(B)*0.4 + SUM(C)*0.1) / COUNT(Serial_number)*1000,2) AS PPM
            FROM  `inspection_report`";

$rResult = mysql_query( $sQuery) or die();
$count = mysql_num_fields($rResult);

// fetch table header
$header = '';
for ($i = 0; $i < $count; $i++){
       $header .= mysql_field_name($rResult, $i)."\t";
       }
// fetch data each row, store on tabular row data
while($row = mysql_fetch_row($rResult)){
       $line = '';
       foreach($row as $value){
              if(!isset($value) || $value == ""){
                     $value = "\t";
              }else{
                     $value = str_replace('"', '""', $value);
                     $value = '"' . $value . '"' . "\t";
                     }
              $line .= $value;
              }
       $data .= trim($line)."\n";
       $data = str_replace("\r", "", $data);

if ($data == "") {
       $data = "\nno matching records found\n";
       }

header("Content-type: application/vnd.ms-excel; name='excel'");
header("Content-Disposition: attachment; filename=exportfile.xls");
header("Pragma: no-cache");
header("Expires: 0");

// output data
echo $header."\n".$data;
?>

beside that, how to make some border for this table?

  • 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-05-18T08:11:20+00:00Added an answer on May 18, 2026 at 8:11 am

    if i want to do : echo “”; which the correct place to
    put it that cant make my script become
    error?

    If youre goign to do it as an html table then i would jsut output liek you normally right a table… for example you might do:

    $rResult = mysql_query( $sQuery) or die();
    $count = mysql_num_fields($rResult);
    
    $html = '<table border="1"><thead><tr>%s</tr><thead><tbody>%s</tbody></table>';
    $thead = '';
    $tbody = '';
    $line = '<tr>%s</tr>';
    for ($i = 0; $i < $count; $i++){      
      $thead .= sprintf('<th>%s</th>',mysql_field_name($rResult, $i));
    }
    
    
    while(false !== ($row = mysql_fetch_row($rResult))){
      $trow = '';
    
      foreach($row as $value){
       $trow .= sprintf('<td>%s</td>', $value);
      }
    
      $tbody .= sprintf($line, $trow);
    
    }
    
    
    header("Content-type: application/vnd.ms-excel; name='excel'");
    header("Content-Disposition: attachment; filename=exportfile.xls");
    header("Pragma: no-cache");
    header("Expires: 0");
    
    print sprintf($html, $thead, $tbody);
    exit;
    

    Youre only getting one row because youre using aggregate functions without a GROUP BY clause. Try adding a GROUP BY on on something that ties each of these “entries” to a specific group like the Lot_no or whatever youre trying to report on.

    As far as the border, you cant do that with CSV which although readbale by Excel, is not actually an Excel format file with all the extras like formatting for borders. To use things like formatting you need to output native Excel or an html table that can be loaded into excel. Take a look at phpexcel if you need to do that.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a php script which accesses a MSSQL2005 database, reads some data from
I have a CSV export script (enrdata_arch.php) which calls information from an existing database
I have to create a script that takes a mySQL table, and exports it
i have some php script and i think this have a lot of mistake.
I have been seeing this in some PHP script: [a-zA-Z0-9_]++ What does the double
Hi have some forms that I want to use some basic php validation (regular
I have inherited some legacy PHP code what was written back when it was
I have some code on my PHP powered site that creates a random hash
I have some code I've written in PHP for consuming our simple webservice, which
I have some old apps written in PHP that I'm thinking of converting to

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.