Forum Moderators: coopster
I have a text file consist of 10 digit phone numbers one number per line like this:
5553332244
5553332244
5553332244
5553332244
etc…
How do I read a text file line by line and populate the data into an array to store it into a MySQL database? My database is below:
CREATE TABLE `friendstel` (
`id` bigint(6) NOT NULL auto_increment,
`tel` varchar(10) NOT NULL default '',
PRIMARY KEY (`id`)
) TYPE=MyISAM AUTO_INCREMENT=1 ;
I successfully done the following to read text file line by line:
<?
$fp = fopen ("friendstel.txt", "r");
while (!feof ($fp)) {
$content = fgets( $fp, 4096 );
echo "$content<br>";
}
fclose ($fp);
?>
How do I store the content of the text file in the MySQL database?
Any help would be appreciated.
Thank you
<?
$sql_static = 'INSERT INTO friendstel (id, tel) VALUES(NULL, ';
$fp = fopen ("mitch888.txt", "r");
while (!feof ($fp)) {
$content = fgets( $fp, 4096 );
$sql = $sql_static . $content .')';
// echo "$sql<br />";
mysql_query($sql);
}
fclose ($fp);
?>
Note: You'll only want to run this once or you will create duplicate phone number entries in your table. Yes, they would have a unique
id key, but duplicate phone numbers nevertheless.
If so, then replace the 'echo' statement with something like this:
$content = trim($content); /* trim() removes trailing whitespace */
$query = "INSERT INTO `friendstel` (tel) VALUES ({$content})";
mysql_query($query) or die("I can not insert into the table because: " . mysql_error());