Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
368 views
in Technique[技术] by (71.8m points)

Loading CSV into MySQL table with PHP

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 '
' IGNORE 1 LINES;
Query OK, 315 rows affected (0.01 sec)
Records: 315  Deleted: 0  Skipped: 0  Warnings: 0

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 '
' IGNORE 1 LINES");   
?>

This script results in an error:

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

The errors log reads:

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

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

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.

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.

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?.

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:

Happy trouble-shooting!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...