I need to do this in PHP.
Do I, just execute the query using mysql_query(); ???
Is that all?
Main Topics
Browse All TopicsHi
How can I do a query from a MySQL database and export to Excel/CSV?
Is there a class or function that can do that?
Thanks.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Hey minichicken,
do you use adodb?
http://phplens.com/lens/ad
That has a function for doing that.
http://www.phpfreaks.com/t
<?php
// DB Connection here
$select = "SELECT * FROM table_name";
$export = mysql_query ( $select ) or die ( "Sql error : " . mysql_error( ) );
$fields = mysql_num_fields ( $export );
for ( $i = 0; $i < $fields; $i++ )
{
$header .= mysql_field_name( $export , $i ) . "\t";
}
while( $row = mysql_fetch_row( $export ) )
{
$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 = "\n(0) Records Found!\n";
}
header("Content-type: application/octet-stream")
header("Content-Dispositio
header("Pragma: no-cache");
header("Expires: 0");
print "$header\n$data";
?>
too bad u want this in PHP, there are a couple of modules in Perl that let you write customized formats for excel.
let me know if you are interested.
==========================
use Spreadsheet::WriteExcel;
$sth= $dbh->prepare($select_quer
$sth->execute();
my $names = $sth->{'NAME'};
my $numFields = $sth->{'NUM_OF_FIELDS'};
### Retrieve the returned rows of data and save them on a excel spreadsheet.
my @row;
# Create a new Excel workbook called XXXX.xls
my $workbook = Spreadsheet::WriteExcel->n
# Add some worksheets
my $sheet1 = $workbook->addworksheet();
$sheet1->activate();
my $header = $workbook->add_format(bold
my $grayline = $workbook->add_format( bg_color => 22 );
my $normalln = $workbook->add_format( bg_color => 9 );
for (my $i = 0; $i < $numFields; $i++) {
$sheet1->write_string(0,$i
}
$j=1;
while ( @row = $sth->fetchrow_array() ) {
for ( my $i = 0; $i <= $#row; $i++) {
if ( defined($row[$i]) ) {
$row[$i] =~ s/\.soups\.com//;
}
if ( $i == 4 || $i == 5 || $i == 6 || $i == 13 ) {
$sheet1->write_string($j,$
} else {
$sheet1->write($j,$i, (defined($row[$i]) ? $row[$i]: " "), ( round($j/2) == 0 ? $normalln : $grayline));
}
$sheet1->write($j,$i,(defi
}
$j++;
}
warn "Data fetching terminated early by error: $DBI::errstr\n"
if $DBI::err;
regards,
esv.
This is exactly what i was looking for...well almost...the only problem I have is that it duplicates columns.
Example:
I am selecting data from table users
select * from users where registered>'2008-03-04'
Example output:
name surname adress city
Paul Paul Jones Jones
John John Brewer Brewer
etc.
Hope you get the point, I tried to find the error in the code, but no success.
Can you please help?
Thanks
Business Accounts
Answer for Membership
by: sajuksPosted on 2005-02-24 at 03:53:26ID: 13391568
try this
mysql -u username -ppassword --batch -e "select * from mytable" mydatabase > mydata.txt
or
SELECT * INTO OUTFILE "c:/mydata.csv"
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY "\n"
FROM my_table;