0

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

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

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

有没有其他人遇到过这个问题,有人可以告诉我我的 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())

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

4

3 回答 3

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

仅仅因为您没有看到任何错误就没有任何意义。您必须询问 mysql 中的错误,例如:

$result = mysql_query($sql) or die(mysql_error());
                           ^^^^^^^^^^^^^^^^^^^^^^
于 2013-03-28T15:24:28.297 回答
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%'
");
于 2013-03-28T15:25:21.617 回答
0

“C:\\jobs.csv”怎么样?我在 Windows 上没有这方面的经验,但我认为它不会像 php 那样用 / 代替 \。

于 2013-03-28T15:23:47.677 回答