gpt4 book ai didi

php - 下载csv中的数据库记录但得到空白行

转载 作者:行者123 更新时间:2023-11-29 20:05:42 25 4
gpt4 key购买 nike

伙计们,我的代码从 mysql 下载 csv 记录,但无论行中存在任何带逗号的值,它都不会下载,而是得到一个空白行

这个解决方案可能是什么

这是我的代码

require_once('config.php');
$u =$_REQUEST['u'];
$cs =$_REQUEST['cs'];
$y =$_REQUEST['y'];
$d =$_REQUEST['d'];
$m =$_REQUEST['m'];
$date = "$y-$m-$d";
$todays = date("d-m-Y");
header('Content-Type: text/csv');
header('Content-Disposition: attachment;filename=Mortgage-'.$u.'-Records-'.$date.'.csv');
//select table to export the data
$select_table=mysql_query("SELECT * FROM records WHERE user ='".$u."' AND status ='".$cs."' AND DATE_FORMAT(posted, '%Y-%m-%d') = '$date' ORDER BY id DESC");
$rows = mysql_fetch_assoc($select_table);
if ($rows)
{
getcsv(array_keys($rows));
}
while($rows)
{
getcsv($rows);
$rows = mysql_fetch_assoc($select_table);
}

// get total number of fields present in the database
function getcsv($no_of_field_names)
{
$separate = '';


// do the action for all field names as field name
foreach ($no_of_field_names as $field_name)
{
if (preg_match('/\\r|\\n|,|"/', $field_name))
{
$field_name = '' . str_replace('', $field_name) . '';
}
echo $separate . $field_name;

//sepearte with the comma
$separate = ',';
}

//make new row and line
echo "\r\n";
}

非常感谢您的时间和帮助

最佳答案

像这样更改代码的第一部分:

require_once('config.php');
$u =$_REQUEST['u'];
$cs =$_REQUEST['cs'];
$y =$_REQUEST['y'];
$d =$_REQUEST['d'];
$m =$_REQUEST['m'];
$date = "$y-$m-$d";
$todays = date("d-m-Y");
header('Content-Type: text/csv');
header('Content-Disposition: attachment;filename=Mortgage-'.$u.'-Records-'.$date.'.csv');
//select table to export the data
$select_table=mysql_query("SELECT * FROM records WHERE user ='".$u."' AND status ='".$cs."' AND DATE_FORMAT(posted, '%Y-%m-%d') = '$date' ORDER BY id DESC");

$i = 0;
while($rows = mysql_fetch_assoc($select_table);)
{
if($i === 0) {
getcsv(array_keys($rows));
}

getcsv($rows);
$i++;
}

并在函数 getcsv 中进行此更改 - 替换此代码:

$field_name = '' . str_replace('', $field_name) . '';

这样:

$field_name = '"' . $field_name . '"';

但不要使用 mysql_query 尝试实现 PDO,因为 mysql lib 已针对 mysql 注入(inject)进行了修剪,并且在新的 php 版本中已弃用。

关于php - 下载csv中的数据库记录但得到空白行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40355819/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com