โค้ด: เลือกทั้งหมด
<?php
// CONFIG //
$host="localhost";
$database="database";
$username="username";
$password="password";
// Must be a writeable location for file
$output_file="export.csv";
// The query to output to CSV
$sql = "select * from tbl1";
// Support for multi-table select
// $sql = "SELECT * FROM tbl2, tbl1 WHERE tbl1.col1 = tbl2.col2";
//---------------------------------------------------
// Connect database
mysql_connect($host,$username,$password);
mysql_select_db($database);
$result=mysql_query($sql);
$output = '';
// Get a list of all the fields in the table
// $fields = mysql_list_fields($database,$table);
// Count the number of fields
$count_fields = mysql_num_fields($result);
// Put the name of all fields to $out.
for ($i = 0; $i < $count_fields; $i++)
{
$field=mysql_fetch_field($result);
$output.= '"'.$field->name.'",';
}
$output .="\n";
// Add all values in the table to $out.
while ($row = mysql_fetch_array($result))
{
for ($i = 0; $i < $count_fields; $i++)
{
$output .='"'.$row["$i"].'",';
}
$output .="\n";
}
// Output the file to the local filesystem. You could append a
// date to the filename to keep a record of the exports.
// Open a new output file
$file = fopen ($output_file,'w');
// Put contents of $output into the $file
fputs($file, $output);
fclose($file);
// This line will stream the file to the user rather than spray it across the screen
header("Content-type: application/octet-stream");
// Internet Explorer support
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Disposition: attachment; filename=report.csv");
header("Pragma: no-cache");
header("Expires: 0");
echo $output;
?>
คือฐานข้อมูลเป็น UTF8 พอ พอต ออกมาเลยไม่เป็นภาษาไทยอะครับ เป็นภาษายึกยือ
มีวิธีแก้ไขไมครับ ติดตรงนี้อย่างเดียว