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.
Loading txt into MySQL with PHP: only integer values import
Moderator: General Moderators
Re: Loading txt into MySQL with PHP: only integer values import
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
That was it, of course. It works.
Thank you VERY much and have a great day.
Thank you VERY much and have a great day.