Exporting data mysql database using php

583 Views Asked by At

I am able to export data from a mysql database into a csv file. It is working if i write /save/to/file but I want file to be saved to where the customer decides. In a previous php the customer enters the directory where they want the csv exported to. This variable is called $pathway. Therefore, in the table information is stored such as username and pathway. I want the csv to be exported based on the username (e.g. if username= Bob, export to $pathway). I cannot import this variable into the export.php. Where is says $sql = "SELECT * INTO OUTFILE '??????' I need to add the variable $pathway but i am not sure how to do it.

if (!$connection) {
die("Database server connection failed.");
die(mysqli_error($db));
} else {

//Attempt to select the database
$dbconnect = mysqli_select_db($connection, $db);
//Check to see if we could select the database

if (!$dbconnect) {
    die("Unable to connect to the specified database!");
} else {
    $sql = "SELECT * INTO OUTFILE '??????'
FIELDS TERMINATED BY '\t' 
LINES TERMINATED BY '\r\n' 
FROM tablename;"
1

There are 1 best solutions below

2
On

You should try like :

SELECT *
FROM tablename
INTO OUTFILE 'CSV_LOCATION'
FIELDS ENCLOSED BY '"' TERMINATED BY ';' ESCAPED BY '"'
LINES TERMINATED BY '\r\n';