?
Solved

Transitioning to PDO/MySQL.  Need help  with CSV import - Pipe delimited

Posted on 2014-12-29
1
Medium Priority
?
164 Views
Last Modified: 2014-12-29
How do I set the delimiter to pipe and use quotes?   I have a field with several commas, so I am changing to pipe delimited.  Each field has quotes as well.

I have tried several different ways to fix.  fgetcsv($handle,""","|",""");




$filename="EP_download.csv";




//open the file
$handle = fopen($filename, "r");
//this is what causes it to skip
fgetcsv($handle,""","|",""");    //How do I set the delimiter to pipe with quotes?



//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 EPdownload (ID, Title, Category, SubCategory, SubSubCategory, Brand, BrandURL, ProductImage, MSRP, MAPPrice, Description, Specification, DealerCost, PageURL, PageTitle, StartingPageURL, DateTime) VALUES(:ID, :Title, :Category, :SubCategory, :SubSubCategory, :Brand, :BrandURL, :ProductImage, :MSRP, :MAPPrice, :Description, :Specification, :DealerCost, :PageURL, :PageTitle, :StartingPageURL, :DateTime)');

		$params = array(':ID'=>$data[0],
						':Title'=>$data[1],
						':Category'=>$data[2],
						':SubCategory'=>$data[3],
						':SubSubCategory'=>$data[4],
						':Brand'=>$data[5],
						':BrandURL'=>$data[6],
						':ProductImage'=>$data[7],
						':MSRP'=>$data[8],
						':MAPPrice'=>$data[9],
						':Description'=>$data[10],
						':Specification'=>$data[11],
						':DealerCost'=>$data[12],
						':PageURL'=>$data[13],
						':PageTitle'=>$data[14],
						':StartingPageURL'=>$data[15],
						':DateTime'=>$data[16]);
		                $statement->execute($params); 
	
}

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
1 Comment
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 40523035
Please see http://php.net/manual/en/function.fgetcsv.php

Arguments are resource $handle [, int $length = 0 [, string $delimiter = "," [,...

So the first-cut of code would look something like this:
$handle = fopen($filename, "r");
if (!$handle) trigger_error("FAILED TO OPEN $filename", E_USER_ERROR);
$arr = fgetcsv($handle, 0, '|');
var_dump($arr);

Open in new window

If that works correctly, then we can add the appropriate loop control to detect EOF on the $handle resource.
0

Featured Post

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

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…
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 look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

752 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