使用PHP将CSV加载到MySQL表中 [英] Loading CSV into MySQL table with PHP

查看:69
本文介绍了使用PHP将CSV加载到MySQL表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用PHP脚本将CSV导入MySQL表中.此SQL命令成功将CSV文件导入到SQL表中:

I am trying to import a CSV into a MySQL table with a PHP script. This SQL command successfully imports the CSV file into the SQL table:

mysql> LOAD DATA LOCAL INFILE 'property_re_1.csv' 
-> REPLACE INTO TABLE `markers`
-> FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' ESCAPED BY '\\'
-> LINES TERMINATED BY '\n' IGNORE 1 LINES;
Query OK, 315 rows affected (0.01 sec)
Records: 315  Deleted: 0  Skipped: 0  Warnings: 0

现在,我想将此SQL命令转换为PHP脚本.这是我编写PHP脚本的尝试:

Now I want to convert this SQL command into a PHP script. Here's my attempt at writing the PHP script:

<?PHP
$dbhost = 'localhost';
$dbuser = 'myusername';
$dbpasswd = 'mypassword';
$db = "db_markers";

$dbh = mysql_connect($dbhost, $dbuser, $dbpasswd) or die("Unable to connect to SQL server");
$my_db = @mysql_select_db($db, $dbh) or die("Unable to select database");

$file = $_SERVER['DOCUMENT_ROOT']."/path/property_re_1.csv";

$result=mysql_query("LOAD DATA LOCAL INFILE '$file' REPLACE INTO TABLE `markers` FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' ESCAPED BY '\\' LINES TERMINATED BY '\n' IGNORE 1 LINES");   
?>

此脚本导致错误:

HTTP错误500(内部服务器错误):服务器尝试满足请求时遇到了意外情况.

HTTP Error 500 (Internal Server Error): An unexpected condition was encountered while the server was attempting to fulfill the request.

错误日志显示为:

[18-Dec-2012 12:37:38] PHP Parse error: syntax error, unexpected    T_CONSTANT_ENCAPSED_STRING in /path/testconnect.php on line 13

在第13行调用$ Result变量,因此错误必须在其中.

The $Result variable is called on line 13, so the error must be therein.

我不确定导致此错误的原因.任何帮助,我们将不胜感激!

I'm not sure what the cause of this error is. Any help is greatly appreciated!

推荐答案

错误的原因是您发送给Web服务器的请求.网络服务器会尝试满足请求,并执行脚本来完成此操作(在您的情况下为PHP脚本).

The cause of the error is the request you've send to the webserver. The webserver tries to fulfill the request and executes scripts to do that, in your case, the PHP script.

PHP脚本现在失败. Web服务器仅知道它发生了故障,但是由于Web服务器不知道任何特定的信息,因此它只会返回所谓的内部服务器错误(内部发生错误),代码500.确切的错误信息是隐藏该错误,因为不会预料到该错误,并且内部信息也不应泄漏给外部世界.

The PHP script now fails. The webserver only knows it failed, but as the webserver does not know anything specific, it will only give back the so called Internal Server Error (the error happened internally), code 500. The exact error information is hidden because the error was not expected and no internal information should be leaked to the outside world.

仅从消息中,没人能说出发生了什么.您需要查看网络服务器的错误日志,并检查内部报告是什么.

From the message alone, nobody can say what happened. You need to look into the error log of your webserver and check what the internal reporting was.

在您的情况下,我认为您的PHP脚本有一个致命错误.您还可以启用PHP错误显示和日志记录,请参见 PHP不显示错误消息.

In your case I would assume that your PHP script has a fatal error. You can also enable PHP error display and logging, see PHP Does Not Display Error Messages.

执行此操作时,可能会看到更多错误消息.常见错误消息在我们的错误参考中进行了解释:参考-此错误在PHP中意味着什么?.

When you do that, you might see more error messages. Common happening error messages are explained in our error reference: Reference - What does this error mean in PHP?.

如果我应该猜测,那是因为您的PHP文件无法解析,所以您会看到此信息.最可能出现以下错误:

If I should place a guess, I'd say you're seeing this because your PHP file does not parse. Most likely the following error:

排除故障!

这篇关于使用PHP将CSV加载到MySQL表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆