Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

A sql error

Posted on 2009-05-13
7
Medium Priority
?
383 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
Independent Software Vendors: 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!

 
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 2000 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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

604 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