Skip to content
Advertisement

Fatal error: Query Failed! SQL: SELECT * INTO OUTFILE

I couldn’t understand why a query work as expected in PhpMyAdmin and from command line (mysql client) and not in PHP.

Fatal error: Query Failed! SQL: SELECT * INTO OUTFILE ‘/var/www/html/domain.dom/tmp/qry_patients_11_11.csv’ FIELDS TERMINATED BY ‘,’ OPTIONALLY ENCLOSED BY ‘”‘ LINES TERMINATED BY ” ” FROM qry_patients ; – Error: in /var/www/html/domain.dom/export_csv.php on line 90

Line 90 and previous:

$where = "";
$tbl_name = "qry_patients"; // table name of the selected db
$FileName = "/var/www/html/domain.dom/tmp/".$tbl_name."_".date("h_i").".csv";
$query = "SELECT * INTO OUTFILE '".$FileName."' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY "n" FROM ".$tbl_name." ".$where.";";
//$query = "SELECT 1;";
$result = mysqli_query($linkDB, $query) OR trigger_error("Query Failed! SQL: $query - Error: ".mysqli_error(), E_USER_ERROR);

After 2 days I’m completely stucked. Please help me! Ilic

Advertisement

Answer

GRANT FILE

The problem was the user of MySQL. It does’nt have FILE permission in MySQL.

With GRANT FILE permission the query works as aspected.

My test from client and in PhpMyAdmin was performed with a full permission user. 🙂 The user I was triyng in PHP has grants only on one DB.

That’s the reason the query does’nt work in my PHP script.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement