将mysql导出到csv时出错

时间:2016-06-15 03:51:00

标签: php mysql csv

新手在这里,我创建了以下查询以从数据库中选择并导出为csv,但我一直收到错误:

$query = "SELECT firstname,lastname,phone,email WHERE firstname = 'Trenton' INTO OUTFILE 'C:/tjtest2.csv' FIELDS TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY '\n' FROM students";
$result = mysql_query($query) or die('error'); 
if($result)
{
    echo"yes!";
}
else
{
    echo"NO!";
};

我的可怕的死亡错误不断出现,我无法弄清楚问题在哪里

4 个答案:

答案 0 :(得分:0)

您需要在条件

之前向学生发送用户
$query = "SELECT firstname,lastname,phone,email form students WHERE firstname = 'Trenton' INTO OUTFILE 'C:/tjtest2.csv' FIELDS TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY '\n'";

答案 1 :(得分:0)

您将条件置于错误的位置,因此请使用下面的代码 -

$query = "SELECT firstname,lastname,phone,email INTO OUTFILE 'C:/tjtest2.csv' FIELDS TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY '\n' FROM students WHERE firstname = 'Trenton'";

<强>更新

通过以下命令获取mysql可以写入的目录路径 -

SHOW VARIABLES LIKE 'secure_file_priv';

它将为您提供类似“C:\ ProgramData \ MySQL \ MySQL Server 5.6 \ Uploads \”的路径,因此请按以下方式使用此路径 -

$query = "SELECT firstname,lastname,phone,email INTO OUTFILE 'C:/ProgramData/MySQL/MySQL Server 5.6/Uploads/tjtest2.csv' FIELDS TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY '\n' FROM students WHERE firstname = 'Trenton'";

答案 2 :(得分:0)

您是否检查过错误日志以获取确切错误?这个文件已经存在了吗?你也可以在你的其他部分内尝试mysql_error(),记得在测试时删除or die,看看你是否找到了一些关于出错的细节

答案 3 :(得分:0)

好的伙计们,我了解到我的查询完全没有问题。问题是我的webhost没有为共享者提供足够高的权限,允许我实现&#34; INTO OUTFILE&#34;。对于那些拥有足够高权限的人来说,显然这个查询应该有效:

$query = "SELECT record FROM table WHERE variable INTO OUTFILE 'filename.csv' FIELDS TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY '\n'"; 
$result = mysql_query($query);
if($result)
{
echo"yes!";
}
else
{
echo mysql_error();
};