无法将数据库作为csv下载

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

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

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

  1. <?php
  2. require 'db_key.php';
  3. $conn = mysqli_connect($servername, $username, $password, $dbname);
  4. $sql_query = mysqli_query($conn, "SELECT * FROM ventas INTO OUTFILE '/tmp/db_downlaod.csv' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\n'");
  5. mysqli_query($conn, $sql) or exit(mysqli_error($conn));
  6. mysqli_close($conn);
  7. header('Content-type: text/csv');
  8. header('Content-disposition: attachment; filename=db_downlaod.csv');
  9. readfile('/tmp/db_downlaod.csv');
  10. unlink('/tmp/db_downlaod.csv');
  11. exit();
  12. ?>

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

deikduxw

deikduxw1#

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

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

视觉效果:

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

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

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

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

相关问题