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.
need to insert lines of multiples files in database
Moderator: General Moderators
- 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
Can't offer much advise unless you can show us what you've done so far.
Re: need to insert lines of multiples files in database
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);
}
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);
}
Re: need to insert lines of multiples files in database
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);
}
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);
}
Re: need to insert lines of multiples files in database
Please include code within <code> tags as it makes it easier to read!
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
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