文字,我的MySQL數據庫我有許多標頭是在這個格式 - 2345X32X12.......
2345=survey id, 32=page id, 12= question id.
列..將數字轉換從MySQL在PHP
在每個標題中包含特定ID組的問題。用PHP我試圖抓住每個ID集的每個問題的文本放置在一個CSV文件。
現在,當我從我的web服務器打開我的csv文件時,我將該格式作爲excel文件列中的標頭(2345X32X12)
;
我想每一欄下的問題是這樣的 - 什麼是你的年齡.........我希望這是有道理的
在另一個表我的數據庫中編號的標題定義問題A1:2345X32X12 B1:你的年齡是?
代碼:
<?php
// connection with the database
//$sid =$_SESSION['sid'];
//echo $sid;
$id=$_GET['sid'];
echo $id;
$dbhost= ""; //your MySQL Server
$dbuser = ""; //your MySQL User Name
$dbpass = ""; //your MySQL Password
$dbname = "y";
$link = mysql_connect($host, $user, $pass);
mysql_select_db($dbname);
$tablename = "survey_".$id; // this is the tablename that you want to export to csv from mysql.
$sql = "Select * from $tablename";
//create code for connecting to mysql
$Connect = @mysql_connect($dbhost, $dbuser, $dbpass)
or die("Couldn't connect to MySQL:<br>" . mysql_error() . "<br>" . mysql_errno());
//select database
$Db = @mysql_select_db($dbname, $Connect)
or die("Couldn't select database:<br>" . mysql_error(). "<br>" . mysql_errno());
//execute query
$result = @mysql_query($sql,$Connect)
or die("Couldn't execute query:<br>" . mysql_error(). "<br>" . mysql_errno());
exportMysqlToCsv($tablename);
function exportMysqlToCsv($tablename,$filename = 'Results.csv'){
$sql_query = "select * from $tablename where id=2";
// Gets the data from the database
$result = mysql_query($sql_query);
$f = fopen('php://temp', 'wt');
$first = true;
while ($row = mysql_fetch_assoc($result)) {
if ($first) {
fputcsv($f, array_keys($row));
$first = false;
}
fputcsv($f, $row);
} // end while
$size = ftell($f);
rewind($f);
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Length: $size");
// 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: attachment; filename=$filename");
fpassthru($f);
exit;
}
?>
我想你是說你希望導出中的列名不是數據庫中的列名是正確的?如果是的話,你想要的名字從哪裏來? – AllInOne