I have the following array with two names:
$x = ['Rodriguez', 'Rodríguez'];
Right now, What I'm trying to do is to insert these values to my Table, Which is like the following
Collation: utf8mb4_unicode_520_ci
Engine: InnoDB
[id(primary_ai) - name(unique)]
And I do connect to the database using:
<?php
try {
$pdo = new PDO("mysql:host=localhost;dbname=database;charset=utf8mb4;", 'root', 'root', [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false
]);
} catch (PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
And I try to insert using like this
$paras = $x
$pdo->prepare("INSERT INTO names (name) VALUES (?), (?)")->execute($paras);
But I keep getting the error
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'Rodríguez' for key 'name'
How can I deal with this encoding problem exactly?
I've tried using utf8_encode()
but the second Name changed to RodrÃguez
, Then tried utf8_decode()
but got Rodr�guez
, Then tried adding PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'"
but didn't solve the duplicate error either.