我无法创建下载CSV文件的脚本。每次我运行脚本时都会中断。我没有收到任何错误。页面一旦到达脚本就停止加载。我环顾四周,找到了一些答案,但似乎都没有。我是php的新手,如果你知道这里发生了什么,我将非常感谢你的解释。提前致谢!
if (isset($_POST['downloadCSV'])) {
$list = null;
include 'classes/Credentials.php';
try
{
// Include globals from credentials.
global $dbname, $host, $user, $pass;
// Set up on database switch
$conn = new PDO("mysql:dbname=$dbname;host=$host", $user, $pass);
$conn->exec("SET CHARACTER SET utf8");
} catch(PDOException $e) {
echo $e->getMessage();
}
// Define and perform the SQL SELECT query
$sql = 'SELECT * FROM rental';
$result = $conn->query($sql);
// If the SQL query is succesfully performed ($result not false)
if($result !== false)
{
$num_fields = mysql_num_fields($result);
$headers = array();
for ($i = 0; $i < $num_fields; $i++) {
$headers[] = mysql_field_name($result , $i);
$fp = fopen('php://output', 'w');
if ($fp && $result) {
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="export.csv"');
header('Pragma: no-cache');
header('Expires: 0');
fputcsv($fp, $headers);
while ($row = $result->fetch_array(MYSQLI_NUM)) {
fputcsv($fp, array_values($row));
}
$conn = null; // Disconnect
die;
}
}
}
}
答案 0 :(得分:0)
尝试
// If the SQL query is succesfully performed ($result not false)
if($result != false) {
您正在使用的比较运算符是检查“不相同”而不是“不相等”