【发布时间】:2015-12-03 08:12:28
【问题描述】:
我在互联网上找到了一段代码,它完全符合我的要求。该代码是关于使用PHP mysql将mysql数据下载为excel表格格式。
问题是我需要将 mysql 转换为 mysqli 函数,它似乎不起作用。 for 循环需要彻底改变。有什么建议吗?
$conn = mysqli_connect("localhost","root","","dvarsam2"); //server,username,password,db
mysqli_query($conn,'SET NAMES utf8');
$setCounter = 0;
$setExcelName = "download_excal_file";
$setSql = "SELECT * FROM wp_applications";
$setRec = mysqli_query($conn,$setSql);
$setCounter = mysql_num_fields($setRec);
for ($i = 0; $i < $setCounter; $i++) {
$setMainHeader .= mysql_field_name($setRec, $i)."\t";
}
while($rec = mysql_fetch_row($setRec)) {
$rowLine = '';
foreach($rec as $value) {
if(!isset($value) || $value == "") {
$value = "\t";
} else {
//It escape all the special charactor, quotes from the data.
$value = strip_tags(str_replace('"', '""', $value));
$value = '"' . $value . '"' . "\t";
}
$rowLine .= $value;
}
$setData .= trim($rowLine)."\n";
}
$setData = str_replace("\r", "", $setData);
if ($setData == "") {
$setData = "\nno matching records found\n";
}
$setCounter = mysql_num_fields($setRec);
//This Header is used to make data download instead of display the data
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=".$setExcelName."_Reoprt.xls");
header("Pragma: no-cache");
header("Expires: 0");
//It will print all the Table row as Excel file row with selected column name as header.
echo ucwords($setMainHeader)."\n".$setData."\n";
【问题讨论】:
-
查看here 了解
mysqli命令