在我的 MYSQL 数据库中,我有许多这种格式的列标题- 2345X32X12.......
2345=survey id, 32=page id, 12= question id.
..
每个标题下都包含该特定 ID 集的问题。使用 PHP,我试图获取每个 ID 集的每个问题的文本以放置在 csv 文件中。
现在,当我从我的网络服务器打开我的 csv 文件时,我得到的格式是(2345X32X12)
excel 文件列中的标题;
我想让每列下的问题看起来像这样 - 你的年龄是多少?............我希望这是有道理的
在我数据库的另一个表中,编号的标题定义了问题 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;
}
?>