Bulk loading with PHP & MySQL

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
stratton
Forum Newbie
Posts: 3
Joined: Wed Jul 24, 2002 3:12 pm

Bulk loading with PHP & MySQL

Post 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)
User avatar
llimllib
Moderator
Posts: 466
Joined: Mon Jul 01, 2002 2:19 pm
Location: Baltimore, MD

Post by llimllib »

put quotes on your SQL string
$sql = 'LOAD DATA LOCAL INFILE "president.txt" INTO TABLE president';
stratton
Forum Newbie
Posts: 3
Joined: Wed Jul 24, 2002 3:12 pm

Post 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
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
gnu2php
Forum Contributor
Posts: 122
Joined: Thu Jul 11, 2002 2:53 am

Post 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.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
stratton
Forum Newbie
Posts: 3
Joined: Wed Jul 24, 2002 3:12 pm

Post 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" :)
Post Reply