Mysql into Excel

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
fried
Forum Newbie
Posts: 16
Joined: Tue Sep 08, 2009 5:43 am

Mysql into Excel

Post by fried »

I'm using Excel stream handler to create an xls file, this works perfectly, except it only enters the first record from the database. I'm sure there is something wrong with my loop. I've tried opening/ closing the file inside and outside of the loop, and the printf shows the complete database. Any help for a newb appreciated. The commented code is from the example given with the code and would not be in a loop.

Code: Select all

$query = "SELECT * FROM newletter";
 
$result = mysql_query($query) or die(mysql_error());
 
while ($resultr = mysql_fetch_array ($result, MYSQL_ASSOC)){
    
    require_once "excel.php";
 
    $export_file = "xlsfile://./datafran.xls";
 
    $fp = fopen($export_file, "wb");
 
    $assoc = array( $resultr);
    
// typically this will be generated/read from a database table
/*$assoc = array(
    array("Sales Person" => "Sam Jackson", "Q1" => "$3255", "Q2" => "$3167", "Q3" => 3245, "Q4" => 3943),
    array("Sales Person" => "Jim Brown", "Q1" => "$2580", "Q2" => "$2677", "Q3" => 3225, "Q4" => 3410),
    array("Sales Person" => "John Hancock", "Q1" => "$9367", "Q2" => "$9875", "Q3" => 9544, "Q4" => 10255),
);*/
 
 
    printf("ID: %s  Email: %s  Ident: %s", $resultr["Id_client"], $resultr["E_mail"], $resultr["Identite"]);
 
 
    fwrite($fp, serialize($assoc));
}
 
fclose($fp);
 
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Mysql into Excel

Post by Christopher »

Move the include_once() and file open to before the loop. And you don't need to convert the record array to an array.
(#10850)
Post Reply