PDA

View Full Version : how to read file and insert database



jr_yeo
08-06-2006, 06:40 PM
dunno where else to put this :p
how to read the contents of a file and insert it into a database?
tnx :cool:

Twey
08-06-2006, 07:33 PM
mysql_query('insert into tablename (columnname) values ("' . mysql_real_escape_string(file_get_contents('somefile')) . '");');

jr_yeo
08-06-2006, 11:04 PM
tnx. how about database results save to file? :D

Twey
08-06-2006, 11:28 PM
$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);

jr_yeo
08-08-2006, 01:15 PM
thnx :cool:

jr_yeo
08-08-2006, 02:52 PM
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

jr_yeo
08-08-2006, 06:42 PM
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.

blm126
08-08-2006, 06:52 PM
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

jr_yeo
08-08-2006, 07:31 PM
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. :(

Twey
08-08-2006, 10:39 PM
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'))) . '");');

jr_yeo
08-09-2006, 12:43 AM
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 ;

jr_yeo
08-11-2006, 09:37 AM
guyz.... any ideas? :p

Twey
08-11-2006, 03:43 PM
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);
?>

jr_yeo
08-12-2006, 02:11 PM
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?

Twey
08-12-2006, 03:44 PM
Oh, I see. You want each line to be a separate record?

jr_yeo
08-13-2006, 05:20 AM
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:

Twey
08-13-2006, 12:50 PM
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);
?>

jr_yeo
08-14-2006, 09:01 AM
will try later :cool: