Page 1 of 1

Bulk loading with PHP & MySQL

Posted: Wed Jul 24, 2002 3:12 pm
by stratton
Hi,

Can anyone tell me how to bulk load data to my database held on my ISP's
Apache server?

I have tried the following script which should point to a .txt file in my CGI bin but when I run it all I get is the following error message -

Parse error: parse error in /files/home3/jdwyer/conter.php on line 13.

I have also enclosed the salient parts of the .txt file I'm hoping to load, which was originally a word document which I saved as a .txt file

My script-
<?

$db_name = "jdwyer_**";

$table_name = "president";

$connection = @mysql_connect("*****", "jdwyer", "2*****8")

or die("Couldn't connect.");

$db = @mysql_select_db($db_name, $connection)

or die("Couldn't select database.");

$sql = LOAD DATA LOCAL INFILE "president.txt" INTO TABLE president;


$result = @mysql_query($sql,$connection)

or die("Couldn't execute query.");

if(!$result)
{
echo "<b>Something not added:</b> ", mysql_error <javascript:QuickRefPop('mysql_error');>();
exit;
}
if($result)
{
mysql_close <javascript:QuickRefPop('mysql_close');>($db);
print "Something added sucessfully!";
}


?>

Part of .txt file -

INSERT INTO president VALUES ('Washington','George',NULL,'Wakefield','VA','1732-02-22','1799-12-14');
INSERT INTO president VALUES ('Adams','John',NULL,'Braintree','MA','1735-10-30','1826-07-04');
INSERT INTO president VALUES ('Jefferson','Thomas',NULL,'Albemarle County','VA','1743-04-13','1826-07-04');

I have set the "Chmod" of the .txt file to 755 & try to run the PHP script by simply typing it's URL into the browser.

Thank you very much for any help.

JD.

(London)

Posted: Wed Jul 24, 2002 3:44 pm
by llimllib
put quotes on your SQL string
$sql = 'LOAD DATA LOCAL INFILE "president.txt" INTO TABLE president';

Posted: Thu Jul 25, 2002 4:08 am
by stratton
Thank you for the help llimllib -Unfortunatly this was all that was thrown back:

Parse error: parse error, expecting `','' or `';'' in /files/home3/jdwyer/cgi-bin/conter.php on line 22

Any idea?

Cheers again.

JD

Posted: Thu Jul 25, 2002 4:34 am
by twigletmac
Try:

Code: Select all

<?php 
$db_name = 'jdwyer_**'; 
$table_name = 'president'; 

$connection = @mysql_connect('*****', 'jdwyer', '2*****8') or die("Couldn't connect."); 
$db = @mysql_select_db($db_name) or die("Couldn't select database."); 

$sql = "LOAD DATA LOCAL INFILE 'president.txt' INTO TABLE president"; 

@mysql_query($sql) or die("Couldn't execute query.".mysql_error()); 

@mysql_close();

print 'Something added sucessfully!'; 

?>
and read this:
http://www.devnetwork.net/forums/viewtopic.php?t=1030

Mac

Posted: Thu Jul 25, 2002 4:41 am
by gnu2php
I'm new to this stuff, but is the problem that you're trying to use JavaScript in your PHP file? Remember that PHP is "server" side, whereas JavaScript is "client" side--meaning that you can't execute JavaScript code in your PHP file, since PHP is executed first.

Posted: Thu Jul 25, 2002 4:46 am
by twigletmac
That's why I posted the link to the sticky in the PHP forum. Basically you can't execute a javascript function from within PHP and you definitely will get parse errors if you put things like:

Code: Select all

mysql_error <javascript:QuickRefPop('mysql_error');> ()
into your PHP code.

Mac

Posted: Thu Jul 25, 2002 9:34 am
by stratton
Thanks for the advise guys - I would not have normally included the "Java" in that section, only having looked up the problem on the"EW script section" I found the script above which came complete with "Java" :)