| | |
Export Mysql data to CSV (also from diferent tables)
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Mar 2009
Posts: 1
Reputation:
Solved Threads: 0
Hello,
Im new to that kind of code so forgive me.
I have 3 diferent tables
players:
id_player
name_player
players_details:
id_player
position
age
players_eval:
id_player
value
wage
You can see that the ‘id_player’ is in all tables. Now, what i want to do is export on csv and if the ‘id_player’ is the same number with the other tables to continew and show the rest of the info i have on the mysql. Also i would like when the 'id_player' to rename to Player ID on the excel file or 'age' to Age etc.
This is the code im using
I really need your help
Thank you
Michael
Im new to that kind of code so forgive me.
I have 3 diferent tables
players:
id_player
name_player
players_details:
id_player
position
age
players_eval:
id_player
value
wage
You can see that the ‘id_player’ is in all tables. Now, what i want to do is export on csv and if the ‘id_player’ is the same number with the other tables to continew and show the rest of the info i have on the mysql. Also i would like when the 'id_player' to rename to Player ID on the excel file or 'age' to Age etc.
This is the code im using
PHP Syntax (Toggle Plain Text)
$table="players"; // this is the tablename that you want to export to csv from mysql. function exportMysqlToCsv($table); { $csv_terminated = "\n"; $csv_separator = ","; $csv_enclosed = '"'; $csv_escaped = "\\"; $sql_query = "select * from $table"; // Gets the data from the database $result = mysql_query($sql_query); $fields_cnt = mysql_num_fields($result); $schema_insert = ''; for ($i = 0; $i < $fields_cnt; $i++) { $l = $csv_enclosed . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, stripslashes(mysql_field_name($result, $i))) . $csv_enclosed; $schema_insert .= $l; $schema_insert .= $csv_separator; } // end for $out = trim(substr($schema_insert, 0, -1)); $out .= $csv_terminated; // Format the data while ($row = mysql_fetch_array($result)) { $schema_insert = ''; for ($j = 0; $j < $fields_cnt; $j++) { if ($row[$j] == '0' || $row[$j] != '') { if ($csv_enclosed == '') { $schema_insert .= $row[$j]; } else { $schema_insert .= $csv_enclosed . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, $row[$j]) . $csv_enclosed; } } else { $schema_insert .= ''; } if ($j < $fields_cnt - 1) { $schema_insert .= $csv_separator; } } // end for $out .= $schema_insert; $out .= $csv_terminated; } // end while $file = "testing"; $filename = $file."_".date("Y-m-d_H-i",time()); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Content-Length: " . strlen($out)); // Output to browser with appropriate mime type, you choose ;) //header("Content-type: text/x-csv"); //header("Content-type: text/csv"); header("Content-type: application/csv"); header("Content-disposition: csv" . date("Y-m-d") . ".csv"); //header("Content-Disposition: attachment; filename=$filename".csv"); header( "Content-disposition: attachment; filename=".$filename.".csv"); echo $out; exit; }
I really need your help
Thank you
Michael
![]() |
Other Threads in the PHP Forum
- Previous Thread: Which content management system
- Next Thread: PHP and Silver Light
| Thread Tools | Search this Thread |
301 apache api array autosuggest beginner beneath broadband broken button cakephp class cms code compression countingeverycharactersfromastring crack cron curl data database date decode display dynamic echo email error fcc file folder form forms freelancing function functions google href htaccess html httppost image include incode integration ip javascript joomla key limit link login match md5 menu method mod_rewrite multiple mysql mysql_real_escape_string oop pagerank paypal pdf php phpvotingscript protocol query random recursiveloop script search searchbox server session sessions sms smtp soap source space sql strip_tags support! survey system table template tutorial upload url variable video virus volume votedown web website window.onbeforeunload=closeme; youtube





