用INTO OUTFILE写.csv

时间:2013-03-28 15:17:27

标签: php mysql csv export

我尝试使用INTO OUTFILE将select语句的结果写入.csv文件。

我现在已经摆弄了几个小时,而且我已经在这里阅读了许多其他相关问题。

出于某种原因,当我运行我的查询时,绝对没有任何反应。该页面不显示任何错误或任何内容,并且未创建该文件。

有没有其他人遇到过这个问题,有人可以告诉我我的SQL是否有问题?

$customers=dbqueryintoarray('
SELECT customer_contacts.Title, customer_contacts.FirstName, 
       customer_contacts.Surname, customer_contacts.Email, 
       customers.LanguageID, customers.DeliveryCountryID 
INTO OUTFILE \'z:/jobs.csv\' 
FIELDS TERMINATED BY \',\' 
OPTIONALLY ENCLOSED BY \'"\' 
LINES TERMINATED BY \'\n\' 
FROM jobs 
JOIN customer_contacts ON jobs.ContactID = customer_contacts.ContactID 
JOIN customers ON jobs.CustomerID = customers.CustomerID 
JOIN equipment ON jobs.EquipmentID = equipment.EquipmentID 
JOIN branches ON jobs.BranchID = branches.BranchID 
WHERE BranchName = "'.$location.'" AND ModelNumber LIKE "%6ES5%"
');

我已经检查过,但我确实拥有权限。我用过

  

或死(mysql_error())

但这不会产生任何错误,它只是从该点向下删除页面中的所有内容。我真的卡住了

3 个答案:

答案 0 :(得分:0)

“C:\\ jobs.csv”怎么样?我在Windows上没有这方面的经验,但我认为它并没有取代\由/作为php。

答案 1 :(得分:0)

不要转义文件路径上的引号:

... INTO OUTFILE 'z:\jobs.csv'

转义它们会阻止MySQL将该文件名视为字符串。它将被视为语法错误,例如:

mysql> select '1' into outfile \'/tmp/z.txt\';
ERROR:
Unknown command '\''.
ERROR:
Unknown command '\''.
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '\'/tmp/z.txt\'' at line 1

仅仅因为你没有看到任何错误意味着什么。您必须ASK以获取mysql中的错误,例如:

$result = mysql_query($sql) or die(mysql_error());
                           ^^^^^^^^^^^^^^^^^^^^^^

答案 2 :(得分:0)

试试这个,用双重配额封装主查询:

$customers=dbqueryintoarray("
SELECT customer_contacts.Title, customer_contacts.FirstName, customer_contacts.Surname, 
customer_contacts.Email, customers.LanguageID, customers.DeliveryCountryID 
INTO OUTFILE 'z:\\jobs.csv' 
FIELDS TERMINATED BY ',' 
OPTIONALLY ENCLOSED BY '\"'
LINES TERMINATED BY '\n'
FROM jobs 
JOIN customer_contacts ON jobs.ContactID = customer_contacts.ContactID 
JOIN customers ON jobs.CustomerID = customers.CustomerID 
JOIN equipment ON jobs.EquipmentID = equipment.EquipmentID 
JOIN branches ON jobs.BranchID = branches.BranchID 
WHERE BranchName = '$location' AND ModelNumber LIKE '%6ES5%'
");