PHP | fputcsv:生成的文件仅填充了字段 header,但没有记录
PHP | fputcsv : generated file only populated with field header, but no records
我不明白为什么这段代码只生成一个包含字段 headers 的文件,但没有记录。
文件内容:
"Article ID",Shoppergroupname,"Promotion Price",VAT-Code,"Article Currency","Promotion Start Date","Promotion End Date"
如您所见,没有记录 "exported" 并且数据库 table 不可用!
顺便说一下,我也想去掉字段 header 引号。
如果有人能帮我解决这个问题,我将不胜感激。提前致谢。
马克
@KateMihalikova 提供的解决方案
// Create connection
$conn = new mysqli($databasehost, $databaseusername, $databasepassword, $databasename);
// Check connection
if (mysqli_connect_errno()) {
exit('Connect failed: '. mysqli_connect_error());
}
echo "Connected successfully | ";
// Create filename
date_default_timezone_set('Europe/Zurich');
$today = date("YmdHis");
$csvname = "WS_PRICE_IMPORT-".$today.".csv";
$csvfullname = '/var/.../'.$csvname;
// create a file pointer connected to the output stream
$output = fopen($csvfullname, 'a+');
// output the column headings
fputcsv($output, array('Article ID', 'Shoppergroupname', 'Promotion Price', 'VAT-Code', 'Article Currency', 'Promotion Start Date', 'Promotion End Date'));
// fetch the data
$sql = "SELECT `Article ID`, `Shoppergroupname`, `Promotion Price`, `VAT-Code`, `Article Currency`, `Promotion Start Date`, `Promotion End Date` FROM jos_temppriceimport";
$result = $conn->query($sql);
if (!$result) {
echo "Unable to execute query in the database : " . mysql_error();
exit;
}
if ($result->num_rows == 0) {
echo "No record found, no record to export in CSV.";
exit;
}
// loop over the rows, outputting them
while ($row = $result->fetch_row()) fputcsv($output, $row);
PS: space字段名由数据提供者提供。
您正在遍历 SQL 语句。您需要循环遍历结果。
while ($row = mysqli_fetch_assoc($result)) fputcsv($output, $row);
您代码中的主要问题是混合了过程 mysql
、过程 mysqli
和面向对象 mysqli
。它们相似,但不能混用。
我们发现主要问题出在 while 循环中。
while ($row = mysql_fetch_assoc($rows)) fputcsv($output, $row);
经过一些迭代,面向对象的符号出现了问题,其中使用对象方法而不是具有属性的函数,但该属性留在那里。 http://phpfiddle.org/main/code/jcbx-7c0n
while ($row = $result->fetch_row($result)) fputcsv($output, $row); // forgotten attribute
while ($row = $result->fetch_row()) fputcsv($output, $row); // working just fine
我不明白为什么这段代码只生成一个包含字段 headers 的文件,但没有记录。
文件内容:
"Article ID",Shoppergroupname,"Promotion Price",VAT-Code,"Article Currency","Promotion Start Date","Promotion End Date"
如您所见,没有记录 "exported" 并且数据库 table 不可用!
顺便说一下,我也想去掉字段 header 引号。
如果有人能帮我解决这个问题,我将不胜感激。提前致谢。
马克
@KateMihalikova 提供的解决方案
// Create connection
$conn = new mysqli($databasehost, $databaseusername, $databasepassword, $databasename);
// Check connection
if (mysqli_connect_errno()) {
exit('Connect failed: '. mysqli_connect_error());
}
echo "Connected successfully | ";
// Create filename
date_default_timezone_set('Europe/Zurich');
$today = date("YmdHis");
$csvname = "WS_PRICE_IMPORT-".$today.".csv";
$csvfullname = '/var/.../'.$csvname;
// create a file pointer connected to the output stream
$output = fopen($csvfullname, 'a+');
// output the column headings
fputcsv($output, array('Article ID', 'Shoppergroupname', 'Promotion Price', 'VAT-Code', 'Article Currency', 'Promotion Start Date', 'Promotion End Date'));
// fetch the data
$sql = "SELECT `Article ID`, `Shoppergroupname`, `Promotion Price`, `VAT-Code`, `Article Currency`, `Promotion Start Date`, `Promotion End Date` FROM jos_temppriceimport";
$result = $conn->query($sql);
if (!$result) {
echo "Unable to execute query in the database : " . mysql_error();
exit;
}
if ($result->num_rows == 0) {
echo "No record found, no record to export in CSV.";
exit;
}
// loop over the rows, outputting them
while ($row = $result->fetch_row()) fputcsv($output, $row);
PS: space字段名由数据提供者提供。
您正在遍历 SQL 语句。您需要循环遍历结果。
while ($row = mysqli_fetch_assoc($result)) fputcsv($output, $row);
您代码中的主要问题是混合了过程 mysql
、过程 mysqli
和面向对象 mysqli
。它们相似,但不能混用。
我们发现主要问题出在 while 循环中。
while ($row = mysql_fetch_assoc($rows)) fputcsv($output, $row);
经过一些迭代,面向对象的符号出现了问题,其中使用对象方法而不是具有属性的函数,但该属性留在那里。 http://phpfiddle.org/main/code/jcbx-7c0n
while ($row = $result->fetch_row($result)) fputcsv($output, $row); // forgotten attribute
while ($row = $result->fetch_row()) fputcsv($output, $row); // working just fine