syedasimmeesaq
asked on
A sql error
I am using this the code below and it is giving me this error
Bad query:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'TERMINATED BY ',' FIELDS ENCLOSED BY '"' LINES TERMINATED BY '\n'' at line 2.
I am not sure why. The code is attached below. Thanks
Bad query:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'TERMINATED BY ',' FIELDS ENCLOSED BY '"' LINES TERMINATED BY '\n'' at line 2.
I am not sure why. The code is attached below. Thanks
function import_files($path) {
$d = dir($path);
echo "Processing $path folder...<br />\n";
while(($file=$d->read())!==false) {
if(($file=='.') or ($file=='..')) continue;
if(is_dir("$path/$file"))
import_files("$path/$file"); # import from sub-folder
else mysql_query(
"LOAD DATA INFILE '$path/$file' INTO TABLE $Table
FIELDS TERMINATED BY ','
FIELDS ENCLOSED BY '\"'
LINES TERMINATED BY '\\n'") or die('Bad query:'.mysql_error());
}
}
this looks like $Table is empty?
ASKER
no I have entry for table
here is the complete code
here is the complete code
$Table = 'dn_table'; # table name
$StartFolder = 'test1'; # folder to import from
function import_files($path) {
$d = dir($path);
echo "Processing $path folder...<br />\n";
while(($file=$d->read())!==false) {
if(($file=='.') or ($file=='..')) continue;
if(is_dir("$path/$file"))
import_files("$path/$file"); # import from sub-folder
else mysql_query(
"LOAD DATA INFILE '$path/$file' INTO TABLE $Table
FIELDS TERMINATED BY ','
FIELDS ENCLOSED BY '\"'
LINES TERMINATED BY '\\n'") or die('Bad query:'.mysql_error());
}
}
# import data
echo "Starting import<br />\n";
import_files($StartFolder);
echo "Done!";
ASKER
tried like this, by hard coding table but still doesnt work
function import_files($path) {
$d = dir($path);
echo "Processing $path folder...<br />\n";
while(($file=$d->read())!==false) {
if(($file=='.') or ($file=='..')) continue;
if(is_dir("$path/$file"))
import_files("$path/$file"); # import from sub-folder
else mysql_query(
"LOAD DATA INFILE '$path/$file' INTO TABLE dn_table
FIELDS TERMINATED BY ','
FIELDS ENCLOSED BY '\"'
LINES TERMINATED BY '\\n'") or die('Bad query:'.mysql_error());
}
}
that would be almost fine, but the $Table variable is not known inside the function, unless you use global:
$Table = 'dn_table'; # table name
$StartFolder = 'test1'; # folder to import from
function import_files($path) {
global $Table;
$d = dir($path);
echo "Processing $path folder...<br />\n";
while(($file=$d->read())!==false) {
if(($file=='.') or ($file=='..')) continue;
if(is_dir("$path/$file"))
import_files("$path/$file"); # import from sub-folder
else mysql_query(
"LOAD DATA INFILE '$path/$file' INTO TABLE $Table
FIELDS TERMINATED BY ','
FIELDS ENCLOSED BY '\"'
LINES TERMINATED BY '\\n'") or die('Bad query:'.mysql_error());
}
}
# import data
echo "Starting import<br />\n";
import_files($StartFolder);
echo "Done!";
I see now. you write:
FIELDS TERMINATED BY ','
FIELDS ENCLOSED BY '\"'
and it must be:
FIELDS TERMINATED BY ',' ENCLOSED BY '\"'
FIELDS TERMINATED BY ','
FIELDS ENCLOSED BY '\"'
and it must be:
FIELDS TERMINATED BY ',' ENCLOSED BY '\"'
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
ok now its not giving error but entering the wrong data. It skips customer name totally and then just mess up the fields for after first line.
here is how I have the data in text file
"cutomer1","","City","AK", "99501","9 07-123-456 7","",""
"cutomer2","","City","AK", "99501","9 07-123-456 7","",""
"cutomer3","","City","AK", "99501","9 07-123-456 7","",""
Thank you for your help
here is how I have the data in text file
"cutomer1","","City","AK",
"cutomer2","","City","AK",
"cutomer3","","City","AK",
Thank you for your help