Solved

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

Posted on 2014-12-30
3
111 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

864 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

21 Experts available now in Live!

Get 1:1 Help Now