[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

mysql to excel question

Posted on 2007-04-08
2
Medium Priority
?
322 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 37

Accepted Solution

by:
momi_sabag earned 1600 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 400 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

649 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