Im trying to download a whole database but i keep getting the error:
Parse error: syntax error, unexpected '' LINES TERMINATED BY '' (T_CONSTANT_ENCAPSED_STRING) in C:\wamp64\www\cookies\download_db.php on line 6
My code:
<?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();
?>
Any kind of help is greatly appreciated
You close the PHP SQL string at the enclosed by
bit then start a new string at the next single quote. You also need to double escape the new line since you are in double quotes and want mysql to get it as \n
. Try:
$sql_query = mysqli_query($conn, "SELECT * FROM ventas INTO OUTFILE '/tmp/db_downlaod.csv' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\\n'");
Visual:
$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
You also are making 2 queries, the second of which is to a variable that is unassigned, $sql
.
Remove:
mysqli_query($conn, $sql) or exit(mysqli_error($conn));
and or possible add that error reporting to the first function call/query execution.