dunno where else to put this :p
how to read the contents of a file and insert it into a database?
tnx :cool:
Printable View
dunno where else to put this :p
how to read the contents of a file and insert it into a database?
tnx :cool:
Code:mysql_query('insert into tablename (columnname) values ("' . mysql_real_escape_string(file_get_contents('somefile')) . '");');
tnx. how about database results save to file? :D
Code:$a = mysql_query('some query or other');
$b = fopen('somefile', 'a');
$c = mysql_fetch_array($a, MYSQL_ASSOC);
$d = array();
foreach($c as $e => $f)
array_push($f, $e);
fwrite($b, implode("\t", $d) . "\n");
do {
fwrite($b, implode("\t", $c) . "\n");
} while($c = mysql_fetch_array($a));
fclose($b);
thnx :cool:
follow up question :cool: what's the difference between include_once() and require_once() in terms of usage? read the php.net manual. need a layperson's explanations. tnx :p
the code works only reading one line of text :pQuote:
Originally Posted by Twey
is there such a thing as while not EOF { do something }
i want something like:
then in the database:Code:first line of text
second line of text
p.s. sorry for not making myself clear on the first post :( tnx.Code:id name
1 first line of text
2 second line of text
include_once issues a warning if it fails. require_once issues an error and stops the execution of the script.
As for the multiple lines
Then just make sure your table has a column with auto incretmentPHP Code:<?php
$lines = file('somefile.txt');
foreach($lines as $num => $line){
mysql_query('insert into tablename (columnname) values ("' . mysql_real_escape_string($line) . '");');
}
?>
no data is being inserted. :(Quote:
Originally Posted by blm126
How peculiar. There is, but it shouldn't be necessary here. Try:Quote:
the code works only reading one line of text
is there such a thing as while not EOF { do something }
Code:mysql_query('insert into tablename (columnname) values ("' . mysql_real_escape_string(str_replace("\n", '\n', file_get_contents('somefile'))) . '");');