简体   繁体   English

PHP MySQL OUTFILE命令

[英]PHP MySQL OUTFILE command

If I use the following in a mysql_query command: 如果我在mysql_query命令中使用以下内容:

SELECT *
FROM mytable
INTO OUTFILE '/tmp/mytable.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n';
  • Where is the tmp file relative to, to the MySQL database somehow or to the PHP file? tmp文件相对于MySQL数据库以某种方式或PHP文件在哪里?
  • If it does not exist will it be created? 如果它不存在会被创建吗?
  • If I would like it to appear 1 folder up from the PHP file which does it, how would I do that? 如果我希望它从PHP文件中出现1个文件夹,那么我该怎么做?

According to The Documentation On Select , it's stored on the server and not on the client: 根据Select Documentation On Select ,它存储在服务器上,而不是存储在客户端上:

The SELECT ... INTO OUTFILE 'file_name' form of SELECT writes the selected rows to a file. SELECT ... INTO OUTFILE'file_name'形式的SELECT将选定的行写入文件。 The file is created on the server host , so you must have the FILE privilege to use this syntax. 该文件是在服务器主机上创建 ,因此您必须具有FILE权限才能使用此语法。 file_name cannot be an existing file, which among other things prevents files such as /etc/passwd and database tables from being destroyed. file_name不能是现有文件,除其他外,它会阻止/ etc / passwd和数据库表等文件被销毁。 As of MySQL 5.0.19, the character_set_filesystem system variable controls the interpretation of the file name. 从MySQL 5.0.19开始,character_set_filesystem系统变量控制文件名的解释。

And, more to the point: 更重要的是:

The SELECT ... INTO OUTFILE statement is intended primarily to let you very quickly dump a table to a text file on the server machine. SELECT ... INTO OUTFILE语句主要用于让您非常快速地将表转储到服务器计算机上的文本文件中。 If you want to create the resulting file on some other host than the server host, you normally cannot use SELECT ... INTO OUTFILE since there is no way to write a path to the file relative to the server host's file system. 如果要在服务器主机之外的其他主机上创建生成的文件,通常无法使用SELECT ... INTO OUTFILE,因为无法相对于服务器主机的文件系统写入文件的路径。

So, don't use it in production to generate CSV files. 因此,请勿在生产中使用它来生成CSV文件。 Instead, build the CSV in PHP using fputcsv : 相反,使用fputcsv在PHP中构建CSV:

$result = $mysqli->query($sql);
if (!$result) {
    //SQL Error
}
$f = fopen('mycsv.csv', 'w');
if (!$f) {
    // Could not open file!
}
while ($row = $result->fetch_assoc()) {
    fputcsv($f, $row);
}
fclose($f);

Where is the tmp file relative to? tmp文件相对于哪里?

A: The file will have the result of the select * from mytable 答:该文件将具有select * from mytable的结果

If it does not exist will it be created? 如果它不存在会被创建吗?

A: yes 答:是的

If I would like it to appear 1 folder up from the php file which does it, how would I do that? 如果我希望它从php文件中出现1个文件夹,它会怎么做?

A: if you want one folder up from the fileYouAreRunning.php then make path like that: "../mytable.csv" 答:如果你想从fileYouAreRunning.php获取一个文件夹,那么就这样制作路径:“../ mytable.csv”

Your current query has an absolute path. 您当前的查询具有绝对路径。 So the outfile will not be relative to anything, but saved to /tmp/mytable.csv . 所以outfile不会相对于任何东西,而是保存到/tmp/mytable.csv

I'd say, the safest bet would be to keep useing absolute paths, so check in your php what your absolute path to the parent is, and then add this to your query using a variable. 我会说,最安全的选择是继续使用绝对路径,因此请检查您的php是什么,您的父路径是什么,然后使用变量将其添加到您的查询中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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