ballhogjoni Posted February 5, 2007 Share Posted February 5, 2007 How do you extract your mysql db to excel using php? Quote Link to comment https://forums.phpfreaks.com/topic/37188-solved-extract-your-mysql-db-to-excel-using-php/ Share on other sites More sharing options...
obsidian Posted February 5, 2007 Share Posted February 5, 2007 Seeing as how Excel will read tab delimited, CSV and other delimited file types, the easiest way is to simply loop over your MySQL database table, echoing each record while implode()ing it with the delimiter of choice. Quote Link to comment https://forums.phpfreaks.com/topic/37188-solved-extract-your-mysql-db-to-excel-using-php/#findComment-177658 Share on other sites More sharing options...
ballhogjoni Posted February 5, 2007 Author Share Posted February 5, 2007 Thank you for your reply. Can you explain what you are saying with code? I do not know what the implode() function is. Quote Link to comment https://forums.phpfreaks.com/topic/37188-solved-extract-your-mysql-db-to-excel-using-php/#findComment-177661 Share on other sites More sharing options...
obsidian Posted February 5, 2007 Share Posted February 5, 2007 Try something like this for starters, and then you can modify it to whatever your circumstances may be: <?php // This page, if included, will require a download of the file you can then open in Excel header("Content-type: application/text"); header("Content-Disposition: attachment; filename=my_export.txt"); $sql = mysql_query("SELECT * FROM my_table"); if (mysql_num_rows($sql) > 0) { while ($row = mysql_fetch_array($sql)) { echo implode("\t", $row) . "\n"; } } exit(); ?> You can change the filename within the Content-Disposition header call to whatever you like. The implode() function, as I am using it, creates a string with all of the values of the row you just pulled from MySQL, joined with tabs (hence, creating a tab delimited file). Hope this helps. Quote Link to comment https://forums.phpfreaks.com/topic/37188-solved-extract-your-mysql-db-to-excel-using-php/#findComment-177664 Share on other sites More sharing options...
ballhogjoni Posted February 5, 2007 Author Share Posted February 5, 2007 Can I do this for multiple tables in one db? Quote Link to comment https://forums.phpfreaks.com/topic/37188-solved-extract-your-mysql-db-to-excel-using-php/#findComment-177673 Share on other sites More sharing options...
ballhogjoni Posted February 5, 2007 Author Share Posted February 5, 2007 Its also duplicating the content. Quote Link to comment https://forums.phpfreaks.com/topic/37188-solved-extract-your-mysql-db-to-excel-using-php/#findComment-177676 Share on other sites More sharing options...
obsidian Posted February 5, 2007 Share Posted February 5, 2007 To answer your first question, you can do this with any SELECT query you can dream up. So, if you want to join tables and export them, no problem. Otherwise, you can create some sort of admin panel that allows for exporting of individual tables. Its also duplicating the content. Can you give some examples? I've just tried it with my tables, and it seems to work fine. Quote Link to comment https://forums.phpfreaks.com/topic/37188-solved-extract-your-mysql-db-to-excel-using-php/#findComment-177698 Share on other sites More sharing options...
ballhogjoni Posted February 5, 2007 Author Share Posted February 5, 2007 1 1 Chris Chris DJKD DJKD Owner Owner 123@cool.com 123@cool.com NE NE 456 456 233 233 4561 4561 I agree I agree 2 2 Mitch Mitch sdlfkj sdlfkj The Man The Man mitch@sdf.net mitch@sdf.net GA GA 770 770 233 233 3808 3808 I agree I agree I copied this straight from the txt file. Quote Link to comment https://forums.phpfreaks.com/topic/37188-solved-extract-your-mysql-db-to-excel-using-php/#findComment-177704 Share on other sites More sharing options...
obsidian Posted February 5, 2007 Share Posted February 5, 2007 Ah, ok... you'll want to use mysql_fetch_row() instead of mysql_fetch_array()... my bad. Quote Link to comment https://forums.phpfreaks.com/topic/37188-solved-extract-your-mysql-db-to-excel-using-php/#findComment-177718 Share on other sites More sharing options...
ballhogjoni Posted February 5, 2007 Author Share Posted February 5, 2007 thx that worked. Now how would I be able to echo multiple tables from my db? Quote Link to comment https://forums.phpfreaks.com/topic/37188-solved-extract-your-mysql-db-to-excel-using-php/#findComment-177723 Share on other sites More sharing options...
ballhogjoni Posted February 5, 2007 Author Share Posted February 5, 2007 I am actually playing around with this code: <?php $sep = "\t"; //tabbed character //start of printing column names as names of MySQL fields /* for ($i = 0; $i < mysql_num_fields($result); $i++) { echo mysql_field_name($result,$i) . "\t"; } for ($i = 0; $i < mysql_num_fields($result1); $i++) { echo mysql_field_name($result1,$i) . "\t"; } print("\n"); //end of printing column names */ //start while loop to get data while($row = mysql_fetch_row($result)) { //set_time_limit(60); // HaRa $schema_insert = ""; for($j=0; $j<mysql_num_fields($result);$j++) { if(!isset($row[$j])) $schema_insert .= "NULL".$sep; elseif ($row[$j] != "") $schema_insert .= "$row[$j]".$sep; else $schema_insert .= "".$sep; } $schema_insert = str_replace($sep."$", "", $schema_insert); $schema_insert = preg_replace("/\r\n|\n\r|\n|\r/", " ", $schema_insert); $schema_insert .= "\t"; print(trim($schema_insert)); print $sep."\n"; } while($row = mysql_fetch_row($result1)) { //set_time_limit(60); // HaRa $schema_insert = ""; for($j=0; $j<mysql_num_fields($result1);$j++) { if(!isset($row[$j])) $schema_insert .= "NULL".$sep; elseif ($row[$j] != "") $schema_insert .= "$row[$j]".$sep; else $schema_insert .= "".$sep; } $schema_insert = str_replace($sep."$", "", $schema_insert); $schema_insert = preg_replace("/\r\n|\n\r|\n|\r/", " ", $schema_insert); $schema_insert .= "\t"; print(trim($schema_insert)); print $sep."\n"; } } mysql_close(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/37188-solved-extract-your-mysql-db-to-excel-using-php/#findComment-177727 Share on other sites More sharing options...
ballhogjoni Posted February 6, 2007 Author Share Posted February 6, 2007 anybody know this? Quote Link to comment https://forums.phpfreaks.com/topic/37188-solved-extract-your-mysql-db-to-excel-using-php/#findComment-177879 Share on other sites More sharing options...
obsidian Posted February 6, 2007 Share Posted February 6, 2007 thanks that worked. Now how would I be able to echo multiple tables from my db? Can you be more specific with what you're after? Are you wanting to output a separate file for each table, or are you simply wanting to loop through several tables and output them all to one file? If it's the latter, do your tables have a similar structure, or are they completely different? There is a lot more detail we need before we can help come up with a valid solution. Quote Link to comment https://forums.phpfreaks.com/topic/37188-solved-extract-your-mysql-db-to-excel-using-php/#findComment-178201 Share on other sites More sharing options...
ballhogjoni Posted February 8, 2007 Author Share Posted February 8, 2007 Thx obsidian, Yes its the latter. As far as I know the tables are similiar. they have an ID that auto_increments and then some other rows. Quote Link to comment https://forums.phpfreaks.com/topic/37188-solved-extract-your-mysql-db-to-excel-using-php/#findComment-179577 Share on other sites More sharing options...
obsidian Posted February 8, 2007 Share Posted February 8, 2007 Since you're just wanting to loop through several tables and output them all to one file, just modify the output above to loop through a predefined array of tables like this: <?php // This page, if included, will require a download of the file you can then open in Excel header("Content-type: application/text"); header("Content-Disposition: attachment; filename=my_export.txt"); // Define tables $tabes = array('table01', 'table02', 'table03', 'table04'); foreach ($tables as $table) { $sql = mysql_query("SELECT * FROM $table"); if (mysql_num_rows($sql) > 0) { while ($row = mysql_fetch_array($sql)) { echo implode("\t", $row) . "\n"; } } } exit(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/37188-solved-extract-your-mysql-db-to-excel-using-php/#findComment-179814 Share on other sites More sharing options...
sayedsohail Posted February 8, 2007 Share Posted February 8, 2007 That's a beauty. thanks Quote Link to comment https://forums.phpfreaks.com/topic/37188-solved-extract-your-mysql-db-to-excel-using-php/#findComment-180048 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.