Helpful Information
 
 
Category: MySQL and other databases
how to read file and insert database

dunno where else to put this :p
how to read the contents of a file and insert it into a database?
tnx :cool:

mysql_query('insert into tablename (columnname) values ("' . mysql_real_escape_string(file_get_contents('somefile')) . '");');

tnx. how about database results save to file? :D

$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

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.

As for the multiple lines


<?php
$lines = file('somefile.txt');
foreach($lines as $num => $line){
mysql_query('insert into tablename (columnname) values ("' . mysql_real_escape_string($line) . '");');
}
?>

Then just make sure your table has a column with auto incretment

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


<?php
$lines = file('somefile.txt');
foreach($lines as $num => $line){
mysql_query('insert into tablename (columnname) values ("' . mysql_real_escape_string($line) . '");');
}
?>

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.

MySQL database Dump :cool:


CREATE TABLE `texter` (
`id` smallint(5) unsigned zerofill NOT NULL auto_increment,
`name` varchar(150) collate latin1_bin NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_bin AUTO_INCREMENT=1 ;

guyz.... any ideas? :p

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);
?>

will try later :cool:










privacy (GDPR)