Hi everyone. I have a csv file and I try to insert in my table (codigo, stud). if(file_exists($ruta.$nombre_archivo.'.'."csv")) { $registro = fopen($ruta.$nombre_archivo.'.'."csv", "r"); $BD = new ConectionDB(); while (($data = fgetcsv($registro, 200, ",")) !== FALSE) { $sth = "INSERT into SC_TEMP (cod, nom) values ('$data[0]', '$data[1]')"; $recordSet = $BD->prepare($sth); $recordSet->bindParam('data[0]', $cod); $recordSet->bindParam('data[1]', $nom); $recordSet->execute(); } } PHP: My csv has this rows: 11111,BRIAN 11252,JOSEPH 52362,MC'DONALD In the third rows the code is truncate. What is the error?
You're doing it wrong. Here, try this: if (file_exists($ruta.$nombre_archivo.'.'."csv")) { $registro = fopen($ruta.$nombre_archivo.'.'."csv", "r"); $BD = new ConectionDB(); while (($data = fgetcsv($registro, 200, ",")) !== FALSE) { $sth = $BD->prepare("INSERT INTO SC_TEMP (cod, nom) VALUES (:cod, :nom)"); //you should really not have a all-caps table name $sth->execute(array(':cod'=>$data[0],':nom'=>$data[1])); } } PHP:
I get this message: ( ! ) Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY093]: Invalid parameter number: parameter was not defined' in Controller.php on line xx ( ! ) PDOException: SQLSTATE[HY093]: Invalid parameter number: parameter was not defined in Controller.php on line xx (line xx is $sth->execute(array(':cod'=>$data[0],':nom'=>$data[1])) Just in case, this is mi ConectionDB.php class ConexionDB extends PDO { public function __construct () { try { parent:: __construct('mysql:host='.DB_HOST.';dbname='.DB_DATA.';charset=utf8', DB_USER, DB_KEY); parent:: setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch (Exception $ex) { die ('Database is not exist'); } } function __destruct(){ } } PHP:
Works fine here. However, you're using Conection in the PHP file, and Conexion in the class (which shouldn't work at all). But yeah, it works here. So I'm guessing you're doing something else than just testing the code I gave you. Here's the code I'm currently testing (I made a test DB so I could test the script): <?php class ConexionDB extends PDO { public function __construct () { try { parent:: __construct('mysql:host=localhost;dbname=cntrl;charset=utf8', 'root','root'); parent:: setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch (Exception $ex) { die ('Database is not exist'); } } function __destruct(){ } } if (file_exists('test.csv')) { echo 'file exists'; $registro = fopen('test.csv', "r"); $BD = new ConexionDB(); while (($data = fgetcsv($registro, 200, ",")) !== FALSE) { $sth = $BD->prepare("INSERT INTO SC_TEMP (cod, nom) VALUES (:cod, :nom)"); //you should really not have a all-caps table name $sth->execute(array(':cod'=>$data[0],':nom'=>$data[1])); } } else { echo 'nope'; } ?> PHP:
I have a one question more about this topic. In case in my csv file there are ñ or Ñ ?? Why this code doesn't work?
For example: In csv file: 123456,garraña 345678,iñaquita In the table (DB) 123456,garra 345678,i Maybe need utf-8 in the insert?
Using this is work: <?php class ConexionDB extends PDO { public function __construct () { try { parent:: __construct('mysql:host=localhost;dbname=xprueba;charset=utf8', 'root','tecsup'); parent:: setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch (Exception $ex) { die ('Database is not exist'); } } function __destruct(){ } } $BD = new ConexionDB(); $sth = $BD->prepare("INSERT INTO xxx (codigo, stud) VALUES (:codigo, :stud)"); $codigo = "666555"; $stud = "mac'pato"; $sth->bindParam(':codigo', $codigo); $sth->bindParam(':stud', $stud); $sth->execute(); $codigo = "777888"; $stud = "Array ñÑáéíóú"; $sth->execute(array(':codigo'=>$codigo, ':stud'=>$stud)) ?> PHP: But, using this, the letters ñ, Ñ and á,é,í,ó,ú doesn't work. <?php class ConexionDB extends PDO { public function __construct () { try { parent:: __construct('mysql:host=localhost;dbname=xprueba;charset=utf8', 'root','tecsup'); parent:: setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch (Exception $ex) { die ('Database is not exist'); } } function __destruct(){ } } $BD = new ConexionDB(); if(file_exists('prueba_caracteres.csv')) { $registro = fopen('prueba_caracteres.csv', "r"); $BD = new ConexionDB(); while (($data = fgetcsv($registro, 200, ",")) !== FALSE) { $sth = $BD->prepare("INSERT INTO xxx (codigo, stud) VALUES (:codigo, :stud)"); $sth->execute(array(':codigo'=>$data[0], ':stud'=>$data[1])); } } ?> PHP: What do you think is the problem?
Probably something wrong with the input data - ie the CSV-file not being saved with UTF-8 - I tested here, copying and pasting the first two entries you posted for the CSV, into the CSV, and ran the script, and it works just fine - hence I would assume that somewhere in the chain there is a file saved with another character encoding.
You are right, I have checked the csv file. The source of that csv file is a excel, the character encoding is Unicode. That is the mistake. Now I have the corrects data. Thank you very much.