我正在尝试使用php从我的数据库中提取数据并将其导出为可以使用excel打开的可下载CSV文件。我能够在使用mysql时执行此操作,但是,许多人建议不要在我的代码中包含mysql语法,因为它被弃用,而是我应该使用mysqli。我已经更改了我的代码,但现在我的代码无效了。有谁知道为什么会这样?
mysql版本(工作版本)`
mysql_connect('localhost', 'xxxxx', 'xxxxx') or die('connect');
mysql_select_db('db') or die('select');
$result = mysql_query('SELECT * bodyshops_master_network') or die('query');
if(mysql_num_rows($result) == 0)
{
die('no data');
}
$fh = tmpfile() or die('tmpfile');
$cols = array_keys(mysql_fetch_assoc($result));
fputcsv($fh, $cols);
mysql_data_seek($result, 0); // set result row pointer back to first row
while($row = mysql_fetch_assoc($result))
{
fputcsv($fh, $row);
}
rewind($fh);
$text = fread($fh, 999999);
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="download.csv"');
header('Content-Length: ' . strlen($text));
echo $text;
exit;
mysqli版本(不工作):
$mysqli = new mysqli("localhost", "xxxxx", "xxxxx", "db");
if (mysqli_connect_errno())
{
printf("Connect failed: ", mysqli_connect_error());
exit();
} else
{
$result = "SELECT * FROM bodyshops_master_network";
if(mysqli_num_rows($result) == 0)
{
die('no data');
}
$fh = tmpfile() or die('tmpfile');
$cols = array_keys($result->fetch_assoc());
fputcsv($fh, $cols);
$result->data_seek(0); // set result row pointer back to first row
while($row = $result->fetch_assoc())
{
fputcsv($fh, $row);
}
rewind($fh);
$text = fread($fh, 999999);
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="download.csv"');
header('Content-Length: ' . strlen($text));
echo $text;
exit;
答案 0 :(得分:0)
检查phpinfo以查看是否启用了mysqli扩展程序。
删除/注释标头调用,以便您以纯HTML格式接收输出,以便您注意是否显示任何消息(由于死或编码错误)或实际获取数据。
另请注意,由于您致电:
,您将丢失您检索的第一条记录的日期$cols = array_keys(mysql_fetch_assoc($result));
分别
$cols = array_keys($result->fetch_assoc());
答案 1 :(得分:0)
什么不起作用?
你有任何错误吗?
文件是否为空,是否有文件下载?
可能未启用错误,请尝试以下操作:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
?>
答案 2 :(得分:0)
我是StackOverflow的新手,我认为这会有所帮助。 (我说西班牙语,我希望你能理解我的英语:D)
我一直在寻找一种简单的方法来使用mysqli并下载一个可以被excel读取而没有UTF-8问题的csv文件(使用ñ,á,ü...)。我没有找到它,所以我自己创建了一个(从Google和StackOverflow的答案中学习),几个小时后我得到了最终有用的东西。
这是一个与数据库连接的类,函数将使用mysqli和PHP执行任何操作。在这种情况下,调用此类(require或include),只需使用“downloadCsv()”函数。
例如,这将是“class.php”文件:
<?php
class DB{
private $con;
//this constructor connects with the database
public function __construct(){
$this->con = new mysqli("Your_Host","Your_User","Your_Pass","Your_DatabaseName");
if($this->con->connect_errno > 0){
die('There was a problem [' . $con->connect_error . ']');
}
}
//create the function that will download a csv file from a mysqli query
public function downloadCsv(){
$count = 0;
$header = "";
$data = "";
//query
$result = $this->con->query("SELECT * FROM Your_TableName");
//count fields
$count = $result->field_count;
//columns names
$names = $result->fetch_fields();
//put column names into header
foreach($names as $value) {
$header .= $value->name.";";
}
}
//put rows from your query
while($row = $result->fetch_row()) {
$line = '';
foreach($row as $value) {
if(!isset($value) || $value == "") {
$value = ";"; //in this case, ";" separates columns
} else {
$value = str_replace('"', '""', $value);
$value = '"' . $value . '"' . ";"; //if you change the separator before, change this ";" too
}
$line .= $value;
} //end foreach
$data .= trim($line)."\n";
} //end while
//avoiding problems with data that includes "\r"
$data = str_replace("\r", "", $data);
//if empty query
if ($data == "") {
$data = "\nno matching records found\n";
}
$count = $result->field_count;
//Download csv file
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=FILENAME.csv");
header("Pragma: no-cache");
header("Expires: 0");
echo $header."\n".$data."\n";
}
?>
创建“class.php”文件后,在本例中,在“download.php”文件中使用该函数:
<?php
//call the "class.php" file
require_once 'class.php';
//instantiate DB class
$export = new DB();
//call function
$export->downloadCsv();
?>
下载后,使用MS Excel打开文件。
我希望这对你有帮助,我想我写的很好,我对文字和代码字段感到不舒服。