1

我试图下载整个数据库,但我不断收到错误:

解析错误:语法错误,第 6 行 C:\wamp64\www\cookies\download_db.php 中的意外 '' LINES TERMINATED BY '' (T_CONSTANT_ENCAPSED_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();
?>

非常感谢任何形式的帮助

4

1 回答 1

0

您在该位关闭 PHP SQL 字符串,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

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

消除:

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

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

于 2018-08-12T16:46:52.947 回答