how can i save the file after SELECT ... INTO OUTFILE 'result.csv'
i use this query to dump table into csv file :
$sql = "SELECT *
INTO OUTFILE 'result.csv'
FIELDS TERMINATED BY ';'
FROM tableName";
i get a result.csv file in the db fold开发者_高级运维er of mysql
how can i save it at root of my site ?
In place of result.csv
provide the path where you want it to be saved:
$sql = "SELECT *
INTO OUTFILE '/path/to/your/site/result.csv'
FIELDS TERMINATED BY ';'
FROM tableName";
this code can export mysql table into csv file , i've tested it with large table
<?php
// tested with success
$db_name = "db_name";
$db_password = "pass";
$db_link = mysql_connect("localhost", "root", $db_password);
mysql_select_db($db_name, $db_link);
mysql_query("SET NAMES UTF8");
$table = "table_name";
function assoc_query_2D($sql, $id_name = false){
$result = mysql_query($sql);
$arr = array();
$row = array();
if($result){
if($id_name == false){
while($row = mysql_fetch_assoc($result))
$arr[] = $row;
}else{
while($row = mysql_fetch_assoc($result)){
$id = $row['id'];
$arr[$id] = $row;
}
}
}else
return 0;
return $arr;
}
function query_whole_table($table, $value = '*'){
$sql = "SELECT $value FROM $table";
return assoc_query_2D($sql);
}
$export_str = "";
$result = query_whole_table($table);
foreach($result as $record){
$export_str .= implode(";",$record) . "\n";
}
// add time to fileName
$date = time();
// output the file
// we can set a header to send it directly to the browser
file_put_contents($date.'_'.$table."_export.csv", $export_str);
?>
精彩评论