Solved

mysql to excel question

Posted on 2007-04-08
2
304 Views
Last Modified: 2013-12-12
I'm exporting info from a mysql db to excel. All works fine.

Is there anyway of adding to the script something which will add three fields/cells to excel which arent part of the db.

for example

add a excel heading of 'sample'  and a cell value of 'X' to each record.

See script below.

<?
$check = mysql_pconnect("sample", "sample", "sample") or die(mysql_error());
mysql_select_db("sample", $check) or die(mysql_error());

$Recordset1 = mysql_query("SELECT date, subtotal as Amount, total as IncTax Amount, companyname as Cofusedmedia, description FROM tblinvoices t1, tblclients t2, tblinvoiceitems t3
where t1.userid = t2.id
and t1.userid = t3.userid", $check) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
$fields = mysql_num_fields($Recordset1);

for ($i = 0; $i < $fields; $i++) {
   $header .= mysql_field_name($Recordset1, $i) . "\t";
}

 do {
    $line = '';
    foreach($row_Recordset1 as $value) {                                            
        if ((!isset($value)) OR ($value == "")) {
            $value = "\t";
        } else {
            $value = str_replace('"', '""', $value);
            $value = '"' . $value . '"' . "\t";
        }
        $line .= $value;
    }
    $data .= trim($line)."\n";
} while($row_Recordset1 = mysql_fetch_assoc($Recordset1));
$data = str_replace("\r","",$data);

if ($data == "") {
    $data = "\n(0) Records Found!\n";  
      }
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=report.xls");
header("Pragma: no-cache");
header("Expires: 0");
print "$header\n$data";
?>
<?php
mysql_free_result($Recordset1);
?>
0
Comment
Question by:Cheryl Lander
2 Comments
 
LVL 37

Accepted Solution

by:
momi_sabag earned 400 total points
ID: 18872049
hi
change your sql to
select 'the title you want here','','','','',''
from yourtable
limit 1
union all
SELECT 'X', date, subtotal as Amount, total as IncTax Amount, companyname as Cofusedmedia, description FROM tblinvoices t1, tblclients t2, tblinvoiceitems t3
where t1.userid = t2.id
and t1.userid = t3.userid

this way you select the header from any table you want
and the unilon will add the result (notice you need the same number of column in both of the select statements thats why i added the empty strings)
you can also select 'X' using sql and use it like another column
0
 
LVL 15

Assisted Solution

by:babuno5
babuno5 earned 100 total points
ID: 18874478
I think this should work

$totalRows_Recordset1 = mysql_num_rows($Recordset1);
$fields = mysql_num_fields($Recordset1);

for ($i = 0; $i < $fields; $i++) {
   $header .= mysql_field_name($Recordset1, $i) . "\t";
}
$header .= "newcoloumn1\t";
$header .= "newcoloumn2\t";
$header .= "newcoloumn3\t";

 do {
    $line = '';
    foreach($row_Recordset1 as $value) {                                            
        if ((!isset($value)) OR ($value == "")) {
            $value = "\t";
        } else {
            $value = str_replace('"', '""', $value);
            $value = '"' . $value . '"' . "\t";
        }
        $line .= $value;
        $line .= "newcoloumn1value1\t";
        $line .= "newcoloumn1value2\t";
        $line .= "newcoloumn1value3\t";
    }
    $data .= trim($line)."\n";
} while($row_Recordset1 = mysql_fetch_assoc($Recordset1));
$data = str_replace("\r","",$data);

if ($data == "") {
    $data = "\n(0) Records Found!\n";  
      }
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
mysql disables rename 4 67
PHP Parse error: syntax error, unexpected '[' 1 23
MySQL Warning Statements when you have a LIMIT clause. 6 26
PHP Syntax Error 4 25
These days socially coordinated efforts have turned into a critical requirement for enterprises.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question