Solved

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

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
php hashing methods 3 50
How do I Import CSV File In my PHP Application 29 76
How do I add date to text file name 15 29
Php variable to be sent back 3 35
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 …
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

751 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