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
mysql_query('insert into tablename (columnname) values ("' . mysql_real_escape_string(file_get_contents('somefile')) . '");');
the code works only reading one line of text :p
is there such a thing as while not EOF { do something }
i want something like:
first line of text
second line of text
then in the database:
id name
1 first line of text
2 second line of text
p.s. sorry for not making myself clear on the first post :( tnx.
include_once issues a warning if it fails. require_once issues an error and stops the execution of the script.
Then just make sure your table has a column with auto incretment
no data is being inserted. :(
the code works only reading one line of text
is there such a thing as while not EOF { do something }How peculiar. There is, but it shouldn't be necessary here. Try:
mysql_query('insert into tablename (columnname) values ("' . mysql_real_escape_string(str_replace("\n", '\n', file_get_contents('somefile'))) . '");');
just like this?
<?php
$conn = mysql_connect('XXXX', 'XXXX', 'XXXX') or die ('Error connecting to mysql');
mysql_select_db('XXXX', $conn) or die ('Error connecting to database');
mysql_query('insert into texter (name) values ("' . mysql_real_escape_string(str_replace("\n", '\n', file_get_contents('text.txt'))) . '");');
?>
still no data is stored.
You realise you've only allowed for 150 bytes of data there? If the file is more than 150 bytes, there will be problems. If not, it's time for debugging code to go in. :)
<?php
$conn = mysql_connect('XXXX', 'XXXX', 'XXXX') or die ('Error connecting to mysql');
mysql_select_db('XXXX', $conn) or die ('Error connecting to database');
$query = 'insert into texter (name) values ("' . mysql_real_escape_string(str_replace("\n", '\n', file_get_contents('text.txt'))) . '");'
mysql_query($query) or die(mysql_error() . '<br><br>' . '<b>Query:</b> ' . $query);
?>
so far, got the impression that u want me to use BLOB here to insert data from file, right?
but what m trying to achieve here is read a file, insert data to the database line by line, and output back the data thru tables and sort the "outputted" data?
Oh, I see. You want each line to be a separate record?
Oh, I see. You want each line to be a separate record?
yeah. i think its something to do with explode() or is it implode()? dunno much about php to read php.net manual efficiently :cool:
You could use explode(), but in this case it's easier to simply use file() to read the file. This returns an array of the lines in the file.
<?php
$conn = mysql_connect('XXXX', 'XXXX', 'XXXX') or die ('Error connecting to mysql');
mysql_select_db('XXXX', $conn) or die ('Error connecting to database');
$query = 'insert into texter (name) values ("%s");';
$lines = file('text.txt');
for($i = 0; $i < count($lines); ++$i)
mysql_query(sprintf($query, mysql_real_escape_string($lines[$i]))) or die(mysql_error() . '<br><br>' . '<b>Query:</b> ' . $query);
?>