Web Hosting Talk







View Full Version : feeding text file to mysql using php


saghir69
12-23-2004, 07:41 AM
can someone write a simple php script that will feed info to a mysql db and then delete that line when its in the db

so the file will look like this


link='www.hjjrjrkk.com'
title='what ever'
description='the description'
link='www.site2.com'
title='title for second site'
description='description for site 2'


i want the script to read first 3 lines add them to the db then remove these 3 lines and move to next 3 lines till the end of file.

can someone please help with this.

azizny
12-23-2004, 10:43 AM
You say:

i want the script to read first 3 lines add them to the db then remove these 3 lines and move to next 3 lines till the end of file.

But the file looks like this:

6 Lines for each site..

So is the format as you listed... then a newline then a different site or is the format different?

Peace,

saghir69
12-23-2004, 10:59 AM
I included the file formated to feed 2 site into 2 new records

first 3 lines are for site 1
and the next 2 for site 2

link='www.hjjrjrkk.com'
title='what ever'
description='the description'
link='www.site2.com'
title='title for second site'
description='description for site 2'

i want link= title= and description= to be the text that doesn't go into the db but is used just to recognise wherethe info to enter in the db


does this make sence? i'm a bit new to this stuff. i can edit code to suit my needs but its a bit hard to start from scratch.

Digitized
12-25-2004, 11:44 AM
Ok well after a few hours of work here it is. If theres anything on it that you want me to change then let me know. The config.php file stores the MYSQL DB Authentication info. I'll allso include the sql for the table I insert the values into. The only thing I changed in the text file was that instead of the vlaues being like.

title='what ever'
They are
what ever

so just remove the "title,description and link =" text and the ' ' marks.

Main Page Code---------------------------------------------------------------
<?php
include('config.php');
mysql_connect($Host,$User,$Pass);
mysql_select_db($DB);
$var = file('file.txt');
$filename = "file.txt";
$num = (count($var) / '3');
print "Will loop $num times<br>";
echo '<pre>';
print "The following where added to the database:<br>";
$total = '0';
$f = '0';
$times = '1';
While( $total <= $num){
while($f < '3'){
print $var[$f];
while($times <= '1'){
$sql = "Insert into sites( link,title,des)
Values( '$var[0]','$var[1]','$var[2]')";
$query = mysql_query($sql) or die(mysql_error());
$times++;
}


$handle = fopen($filename, "r+");
$contents = fread($handle, filesize($filename));
$contents = str_replace($var[$f], "", $contents);
rewind($handle);
fwrite($handle, $contents);
ftruncate($handle,ftell($handle));
fclose($handle);
$f++;
}
$total++;
}
echo '</pre>';
?>
End Code---------------------------------------------------------------------

SQL For 'Sites' Table--------------------------------------------------------
#
# Table structure for table `sites`
#

CREATE TABLE sites (
id int(255) NOT NULL auto_increment,
link varchar(255) NOT NULL default '',
title varchar(255) NOT NULL default '',
des varchar(255) NOT NULL default '',
UNIQUE KEY id (id)
) TYPE=MyISAM;
End SQL-----------------------------------------------------------------------

Ok thats is.

-Sam (Digitized Administrator)

saghir69
12-25-2004, 12:28 PM
Digitized thanks a lot, thats great help. i was getting tiered of using a submition form to submit each site on its own.

can you just add 1 more thing 4 me.

can u make the the script read first 5 lines at a time,

the 2 extra lines are to be inserted into catid and subid fields. thank

i will try to edit the code and probably will make it work. but if its not too much hasle maybe you can can do it? thanks

Digitized
12-26-2004, 03:25 AM
Do you think you can send me the sql file for the new table or post it on here.

-Sam (Digitized Administrator)