无法将数据库下载为 CSV

Unable to download database as CSV

我正在尝试下载整个数据库,但我一直收到错误消息:

Parse error: syntax error, unexpected '' LINES TERMINATED BY '' (T_CONSTANT_ENCAPSED_STRING) in C:\wamp64\www\cookies\download_db.php on line 6

我的代码:

<?php 
    require 'db_key.php';

    $conn = mysqli_connect($servername, $username, $password, $dbname);

    $sql_query = mysqli_query($conn, "SELECT * FROM ventas INTO OUTFILE '/tmp/db_downlaod.csv' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\n'");

    mysqli_query($conn, $sql) or exit(mysqli_error($conn));

    mysqli_close($conn);

    header('Content-type: text/csv');
    header('Content-disposition: attachment; filename=db_downlaod.csv');
    readfile('/tmp/db_downlaod.csv');
    unlink('/tmp/db_downlaod.csv');
    exit();
?>

非常感谢任何形式的帮助

您在 enclosed by 位关闭 PHP SQL 字符串,然后在下一个单引号处开始一个新字符串。您还需要对新行进行双重转义,因为您在双引号中并且希望 mysql 将其作为 \n。尝试:

$sql_query = mysqli_query($conn, "SELECT * FROM ventas INTO OUTFILE '/tmp/db_downlaod.csv' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\n'");

视觉:

$sql_query = mysqli_query($conn, "SELECT * FROM ventas INTO OUTFILE '/tmp/db_downlaod.csv' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\n'");
                                                                                                                                 closes PHP>^^<starts a new single quoted PHP string

您还进行了 2 次查询,其中第二次是针对未分配的变量,$sql

删除:

mysqli_query($conn, $sql) or exit(mysqli_error($conn));

和/或可能将该错误报告添加到第一个函数 call/query 执行中。