Solved

A sql error

Posted on 2009-05-13
7
381 Views
Last Modified: 2013-12-12
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

0
Comment
Question by:syedasimmeesaq
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24379533
this looks like $Table is empty?
0
 
LVL 9

Author Comment

by:syedasimmeesaq
ID: 24379582
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

0
 
LVL 9

Author Comment

by:syedasimmeesaq
ID: 24379614
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

0
Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24379618
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

0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24379638
I see now. you write:

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

and it must be:

FIELDS TERMINATED BY ',' ENCLOSED BY '\"'
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 24379640
0
 
LVL 9

Author Comment

by:syedasimmeesaq
ID: 24379795
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
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question