无法将数据库作为csv下载

pod7payv  于 2021-06-23  发布在  Mysql
关注(0)|答案(1)|浏览(307)

这个问题在这里已经有答案了

php解析/语法错误;以及如何解决这些问题(18个答案)
两年前关门了。
我试图下载整个数据库,但我不断得到错误:
分析错误:语法错误,意外的“”行被c:\wamp64\www\cookies\download\u db.php第6行中的“”(t\u constant\u encapsed\u string)终止
我的代码:

<?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();
?>

任何形式的帮助都非常感谢

deikduxw

deikduxw1#

enclosed by 位,然后在下一个单引号处开始一个新字符串。您还需要对新行进行双转义,因为您在双引号中,希望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

您还进行了两个查询,其中第二个是对未赋值的变量的查询, $sql .
删除:

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

和或可能将错误报告添加到第一个函数调用/查询执行中。

相关问题