PHP PDO Basics! Escaping/Unescaping Special Characters -


im starting out pdo methods , have managed create table , insert 10 test records .csv file. checked table in phpmyadmin , noticed fields containing speech marks (in csv file) enclosed double quotes "original csv value ""would"" this" - correct or have done wrong?

if thats correct, how make output display without double quotes?

i hope makes sense? have read loads of q&a's similar problem none of them explaining should happening in db , subsequently how pull data out , display when special characters involved. can pull out , display - quotes present. help! please!


got there in end. help. correct, csv file contained escaped characters working should not how wanted to. final code below, hope others in future. removes double quotes etc csv data , stores them in table how intended :-)

<?php require_once ('mysql_connect.php'); $databasefile = fopen('products.csv', 'r'); if ($databasefile === false) {     die('could not open csv file!'); } fgetcsv($databasefile, 0, "|"); // removes csv header line  $stmt = $dbconnection->prepare('insert products (id,category,title,price,shipping,live,height,width,depth) values (null, ?, ?, ?, ?, ?, ?, ?, ?)');      while (($csvrow = fgetcsv($databasefile, 500, "|")) !== false) {         $stmt->bindvalue(1, $csvrow[0]);         $stmt->bindvalue(2, $csvrow[1]);         $stmt->bindvalue(3, $csvrow[2]);         $stmt->bindvalue(4, $csvrow[3]);         $stmt->bindvalue(5, $csvrow[4]);         $stmt->bindvalue(6, $csvrow[5]);         $stmt->bindvalue(7, $csvrow[6]);         $stmt->bindvalue(8, $csvrow[7]);         $stmt->execute();     }  fclose($databasefile); $dbconnection = null; ?> 

what see csv-style escaping. in csv, " escaped writing twice (""). fact see in database means lines , fields csv file not correctly unescaped (transformed) csv regular text before inserted.

you can parse (and automatically unescape) csv strings using built-in php functions str_getcsv() (if have csv file in string variable) , fgetcsv() (for when csv still in file).

see examples in linked functions better understanding of how use them.

once have values csv (freshly parsed , unescaped), can insert them in database , "" should have become regular " again.

remember that, insert them in database, need database-style escaping again (which different csv style escaping). easiest way via pdo prepared statements.

an example:

$filehandle = fopen('somecsvfile.csv', 'rb'); if ($filehandle === false) {     die('could not open file!'); }  $db = new pdo('mysql:dbname=test;host=localhost', 'user', 'pass'); $db->setattribute(pdo::attr_errmode, pdo::errmode_exception);  $stmt = $db->prepare('insert testtable(column1, column2, column3) values (?, ?, ?)');  while (($csvrow = fgetcsv($fh) !== false) {     if (count($csvrow) !== 3) {         echo 'encountered row wrong number of fields.';         continue;     }      $stmt->bindvalue(1, $csvrow[0]);     $stmt->bindvalue(2, $csvrow[1]);     $stmt->bindvalue(3, $csvrow[2]);     $stmt->execute(); } 

Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -