Page 1 of 1

Loading txt into MySQL with PHP: only integer values import

Posted: Mon Oct 06, 2008 7:38 am
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.

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

Posted: Mon Oct 06, 2008 8:45 am
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.....);
 

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

Posted: Mon Oct 06, 2008 10:54 am
by mb2442
That was it, of course. It works.
Thank you VERY much and have a great day.