Click to See Complete Forum and Search --> : Read text file and store in variable


SunnyVu
03-06-2007, 12:41 AM
Hi,

I am trying to insert a record into the database. What I want to do is read everything in the text file and store it in a third variable ($result), then after that, I insert this in a database table (text). Please help!

$strTest1 = "test1"; - First variable
$strTest2 = "test2"; - Second variable.

$result = Third variable, I would like to read all the data in the text file below (which is echo correctly when I try the codes below)
$tmp = "tmp1.txt";
$data = file($tmp) or die('Could not read file!');
// loop through array and print each line
foreach ($data as $line) {
echo $line."<br>";
}

My SQL syntax would be:
mysql_query("INSERT INTO scriptactlog (login, scriptname, scriptoutput) VALUES ('$strTest1', '$strTest2', '$result')");

Thanks in advance

NogDog
03-06-2007, 01:51 AM
file_get_contents (http://www.php.net/file_get_contents)()

SunnyVu
03-06-2007, 03:35 AM
Thanks NogDog, Will try that.

I able to put into a variable($result) and echo it fine, but couldn't insert into the record. If I just change $result = "Testing", then the same sql insert query will able to insert. So WEIRD, any idea?

$tmp = "03.06.07_18839.txt";
$data = file($tmp) or die('Could not read file!');
// loop through array and print each line

$result = "";
foreach ($data as $line) {
// echo $line."<br>";
$result = $result.$line."<br>";
}
echo $result;

mysql_select_db("NSM", $MySQLconn);
mysql_query("INSERT INTO scriptactlog (login, scriptname, scriptoutput) VALUES ('test1', 'test2', '$result')");

//mysql_query("INSERT INTO scriptactlog (login, scriptname, scriptoutput) VALUES ('testing1', 'testing2', 'testing3')");
mysql_close($MySQLconn);

?>

NogDog
03-06-2007, 04:01 AM
Be sure to run the text from the file through mysql_real_escape_string (http://www.php.net/mysql_real_escape_string)() before trying to use it in a query.

SunnyVu
03-06-2007, 10:41 PM
Thanks, will try