Error SQLSTATE [HY000]: General error: 1366 inserting CVS with PHP Mysql?

-1

I have a page that lets you import CVS to Mysql database, but when in the CVS there is a symbol that occupies 4 bits instead of 3, I get this error:

SQLSTATE[HY000]: General error: 1366 Incorrect string value: '\x96
0014...'

And the import of the rows that contain some symbol as bar - , slash / or other, is suspended.

How do I solve it in order to import everything? From collation I have utf8_general_ci , I use PDO prepared statements to make INSERT . My code:

 $sql = "
INSERT INTO 'lithuania_customers_2016'
('customer_db_id', 'user_id', 'user_email')
VALUES (?, ?, ?);";
$stmt = $pdo->prepare($sql);
$stmt->execute(array(
   $emapData[0],
   $emapData[1],
   $emapData[2]
));
    
asked by Lukas 27.04.2017 в 11:39
source

1 answer

2

When you create the connection to mysql you must specify the character set it is not enough to put the tables and fields in utf8. The connection also has its own character set.

Something like this:

<?php
$db = new pdo(
'mysql:host=127.0.0.1;port=3306;dbname=mysql;charset=utf8',
'user',
'password',
array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8") 
);

From mysql it is also possible to set the value to utf8 in the environment variables.

    
answered by 27.04.2017 / 15:51
source