Loading txt into MySQL with PHP: only integer values import

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mb2442
Forum Newbie
Posts: 7
Joined: Mon Oct 06, 2008 7:17 am

Loading txt into MySQL with PHP: only integer values import

Post by mb2442 »

Greetings from a Newbie, and my thanks. I am trying to automate the loading of a syslog text file into mysql using php. After connecting to the db, I'm using this code:

$fileHandle= @fopen($filename, "r");
if ($fileHandle) {
while (!feof($fileHandle)) {
$line = fgets($fileHandle, 4096);
$dataArray = preg_split("/\s+/", $line);
mysql_query('INSERT INTO `syslogscans`.`tablename` (`field1`, `field2`, etc....) VALUES ($dataArray[1] . ', ' . $dataArray[2] . etc.....);
}
fclose($fileHandle);
}

THE PROBLEM: All fields in the text file containing only integer values DO import (some of those being varchar some int). All fields in the text file containing any string data will NOT import. Any suggestions as to cause & solution would be greatly appreciated. Thank you.
PietM
Forum Newbie
Posts: 8
Joined: Mon Oct 06, 2008 2:56 am

Re: Loading txt into MySQL with PHP: only integer values import

Post by PietM »

If you insert string or date fields, you have to surround them with quotes.

Code: Select all

 
mysql_query('INSERT INTO `syslogscans`.`tablename` (`intfield`, `stringfield`, etc....) VALUES ($dataArray[1] . ', \\'' . $dataArray[2] . '\\'' . etc.....);
 
mb2442
Forum Newbie
Posts: 7
Joined: Mon Oct 06, 2008 7:17 am

Re: Loading txt into MySQL with PHP: only integer values import

Post by mb2442 »

That was it, of course. It works.
Thank you VERY much and have a great day.
Post Reply