Solved

Transitioning to PDO/MySQL.  Need help  with CSV import error

Posted on 2014-12-30
3
112 Views
Last Modified: 2014-12-30
I have worked this many ways.  I am getting the following errors:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 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 'Option) VALUES('02802', 'Color', 'Blue', '')'

PDOStatement->execute(Array) #1 {main} thrown



Data looks  like this:

02801|Color|"Burgundy"                  
02802|Color|"Blue"                  
02808|Color|"Burgundy"                  
02890|Color|"Burgundy"                  
02891|Color|"Blue"                  
02892|Color|"Burgundy"                  
02893|Color|"Blue"                  
07200|Length|"26"""                  
07201|Length|"21"""                  



$filename="DealerCatalogOptionExport.csv";




//open the file
$handle = fopen($filename, "r");
//this is what causes it to skip
fgetcsv($handle,"'","|");



//begin looping through the lines
while (($data = fgetcsv($handle,"'","|")) !== FALSE)
{


// loop all column values and escape special characters
 foreach ($data as $key => $value){
  $data[$key] = $value;
}


$statement = $conn->prepare('INSERT IGNORE INTO options (ParentSKU, SKU, Choice, Option) VALUES(:ParentSKU, :SKU, :Choice, :Option)');

		$params = array(":ParentSKU" => isset($data[0])?$data[0]:"",
						":SKU" => isset($data[1])?$data[1]:"",
						":Choice" => isset($data[2])?$data[2]:"",
						":Option" => isset($data[3])?$data[3]:"");
		$statement->execute($params); 
	
echo $data[0]."&nbsp;".$data[1]."&nbsp;".$data[2]."&nbsp;".$data[3]."<BR>";
	
	
	}

Open in new window

0
Comment
Question by:lawrence_dev
  • 2
3 Comments
 
LVL 58

Accepted Solution

by:
Gary earned 500 total points
ID: 40524232
PDO and prepared statements wouldn't give you this error as the values are not part of the sql.

right syntax to use near 'Option) VALUES('02802', 'Color', 'Blue', '')'

Open in new window


So are you sure the error is not coming from somewhere else?
0
 

Author Comment

by:lawrence_dev
ID: 40524292
Thanks for your help Gary!!

Actually, 'Option' is a reserved word and cannot be used.  I changed it to 'Opt' and it worked fine.
0
 

Author Closing Comment

by:lawrence_dev
ID: 40524293
Thanks!
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
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 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 …

808 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