Link to home
Start Free TrialLog in
Avatar of syedasimmeesaq
syedasimmeesaqFlag for United States of America

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

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());
  }
}

Open in new window

Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

this looks like $Table is empty?
Avatar of syedasimmeesaq

ASKER

no I have entry for table
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!";

Open in new window

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());
  }
}

Open in new window

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!";

Open in new window

I see now. you write:

FIELDS TERMINATED BY ','
          FIELDS ENCLOSED BY '\"'

and it must be:

FIELDS TERMINATED BY ',' ENCLOSED BY '\"'
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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","907-123-4567","",""
"cutomer2","","City","AK","99501","907-123-4567","",""
"cutomer3","","City","AK","99501","907-123-4567","",""

Thank you for your help