Solved

A sql error

Posted on 2009-05-13
7
375 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
  • 4
  • 3
7 Comments
 
LVL 142

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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 142

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 142

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 142

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

705 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now