简体   繁体   中英

Export MySQL table into a CSV file in specified folder ( which makes it dynamic )

I want to export all my data from a table to a csv file in the same folder my website is at.

mysqli_query($connect,"SELECT *
FROM grades
INTO OUTFILE 'file.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '\"'
LINES TERMINATED BY '\n'");

There is no problem when it creates the file, but it saves it at the data location of MySQL which makes it harder/impossible to acces if you are using a remote server where you are not the admin of.

Does someone know how I can save the file in the same folder where my website is at? ( INTO OUTFILE C:\\server\\http\\mywebsite is not acceptable )

Thanks!

this query is executing on the server you connect to, so it will be saved on it, you can select it and then save it

$result = mysqli_query($connect,"SELECT * FROM grades");

    while ($row = mysqli_fetch_assoc($result)) {
       $row2[]=implode(",",$row);
    }

file_put_contents ("file.csv",implode("\r\n",$row2));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM