need to insert lines of multiples files in database

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Jimmyh
Forum Newbie
Posts: 7
Joined: Mon Mar 24, 2008 12:52 pm

need to insert lines of multiples files in database

Post by Jimmyh »

I have an application in php which will look into a folder and if it finds files it will need to insert the lines of all the files in a database, i have two problems:
1) I am not able to insert the content or all lines of one file to the database, he inser does not work.
2) Since the folder can have more than one file, i will need to insert the content of all files into the database also i have to respect the time that the files was in the folder(i.e. if a file was at the folder at 10 am its lines suppose to be inserted into the database before a file which was at the folder at 11 am)

I am able to print the lines of the files into an array, but the insert does not work for me.

any help out will be well received.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: need to insert lines of multiples files in database

Post by John Cartwright »

Can't offer much advise unless you can show us what you've done so far.
Jimmyh
Forum Newbie
Posts: 7
Joined: Mon Mar 24, 2008 12:52 pm

Re: need to insert lines of multiples files in database

Post by Jimmyh »

Jcart below is what i was trying to do:

dbc= @mysql_connect(localhost,root) OR die('Could not connect to the database:'.mysql_error());
mysql_select_db(DB_NAME) OR die ('could not select the database:'.mysql_error());
$dirPath="C:\\text";
if ($handle = opendir($dirPath)) {

while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
if (is_dir("$dirPath/$file")) {

echo " $file is a folder<br>";
} else {
$myfile="$dirPath\\$file";

$req = 'insert into tablename (column_name) values ("%s");';

for($i = 0; $i < count($myfile); ++$i){
mysql_query(sprintf($req, mysql_real_escape_string($myfile[$i]))) or die(mysql_error() . '<br><br>' . '<b>Query:</b> ' . $req);

}
}
fclose($fh);
}
}
closedir($handle);
}
Jimmyh
Forum Newbie
Posts: 7
Joined: Mon Mar 24, 2008 12:52 pm

Re: need to insert lines of multiples files in database

Post by Jimmyh »

sorry this is the code which does not want to insert:

also how can you explain me how to handle the part 2 (More than one files)of my the Post

$dirPath="C:\\text";

if ($handle = opendir($dirPath)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
if (is_dir("$dirPath/$file")) {

echo "$file is a folder<br>";
} else {
$myfile="$dirPath\\$file";
$fh= fopen($myfile,'r');
$kpt=array_unique(file("$myfile"));
$req = 'insert into tablename (column_name) values ("%s");';

for($i = 0; $i < count($kpt); ++$i){
mysql_query(sprintf($req, mysql_real_escape_string($kpt[$i]))) or die(mysql_error() . '<br><br>' . '<b>Query:</b> ' . $req);


}

}

fclose($fh);

}
}

closedir($handle);

}
AMCH
Forum Commoner
Posts: 31
Joined: Sun Mar 30, 2008 4:39 pm

Re: need to insert lines of multiples files in database

Post by AMCH »

Please include code within <code> tags as it makes it easier to read! :D

Do you have any psuedocode for this?

Also, what is the main purpose of it all as there may be a much simpler way of doing it?

Kind Regards
AMCH :D
Post Reply