[PHP] Mass MySQL INSERT

2004-11-28 Thread Travis Conway
I am fairly new to the concept of working wity mysql and php.  I am looping 
thru a text file full of county names and attempting to add these to a database.

Now the table exists and if I enter the SQL statement manually against the 
database it works.  The user also has read and write permissions to the table 
and the columns in the table.

Here is my php code:
 $lines = file(/tmp/new1.txt);
 foreach ($lines as $line_num = $line) {
  echo($line . '... ');
   $link = mysql_connect('naabe', 'naabe', 'x')
or die('could not connect: ' . mysql_error());
// I use substr to remove the space at the end of every line.
   $sql = INSERT INTO countries (country) VALUES (' . 
substr($line,0,strlen($line)-1) . ');
   echo ($sql . ' ...br');
   mysql_query($sql);
   mysql_close();
  flush();
 }

This is how I configured php4.3.9
./configure --enable-so --with-mysql=/usr/local/mysql --enable-filepro 
--with-apx2=/usr/local/apache2/bin/apxs --with-jpg --with-png --with-xml

It will print to the screen the line and the SQL statement correctly.  The 
password is correct.  The database name is naabe, the user is naabe.

Anyone know what I could be doing wrong?

Trav

Re: [PHP] Mass MySQL INSERT

2004-11-28 Thread Jason Wong
On Monday 29 November 2004 03:54, Travis Conway wrote:
 I am fairly new to the concept of working wity mysql and php. 

Questions regarding the use of databases with PHP should be posted to the 
php-db list.

 I am looping 
 thru a text file full of county names and attempting to add these to a
 database.

 Now the table exists and if I enter the SQL statement manually against the
 database it works.  The user also has read and write permissions to the
 table and the columns in the table.

 Here is my php code:
  $lines = file(/tmp/new1.txt);
  foreach ($lines as $line_num = $line) {
   echo($line . '... ');
$link = mysql_connect('naabe', 'naabe', 'x')

The connection should be made only once outside of the loop.

 or die('could not connect: ' . mysql_error());

Using mysql_error() is good, use it more often ...

 // I use substr to remove the space at the end of every line.
$sql = INSERT INTO countries (country) VALUES (' .
 substr($line,0,strlen($line)-1) . '); echo ($sql . ' ...br');
mysql_query($sql);

... like here. If your query barfs you'll want to know about it.

-- 
Jason Wong - Gremlins Associates - www.gremlins.biz
Open Source Software Systems Integrators
* Web Design  Hosting * Internet  Intranet Applications Development *
--
Search the list archives before you post
http://marc.theaimsgroup.com/?l=php-general
--
/*
If you see an onion ring -- answer it!
*/

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php