Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2014-12-30
3
Medium Priority
?
122 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
[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
  • 2
3 Comments
 
LVL 58

Accepted Solution

by:
Gary earned 2000 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
This article discusses how to implement server side field validation and display customized error messages to the client.
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 dynamically set the form action using jQuery.

661 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