utf8mb3_general_ci into utf8mb4_general_ci

Issue

I was doing a comment section and I wanted to store the comments in the database but i get this error: Uncaught PDOException: SQLSTATE[HY000]: General error: 3988 Conversion from collation utf8mb3_general_ci into utf8mb4_general_ci impossible for parameter

try {
    $pdo = new PDO($db_config, $config['db_user'], $config['db_password'], [
        PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"
    ]);

    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
} catch (PDOException $e) {
    exit("Impossibile connettersi al database: " . $e->getMessage());
}
$check = $pdo->prepare($query);
  $check->bindParam(':uid', $id, PDO::PARAM_STR);
  $check->bindParam(':date', $date, PDO::PARAM_STR);
  $check->bindParam(':message', $message, PDO::PARAM_STR);
  $check->execute();
CREATE TABLE `comments`(
`cid` int(11) not null  AUTO_INCREMENT PRIMARY KEY,
`uid` varchar(128) not null,
`date` datetime not null,
`message` TEXT not null) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

Solution

resolved like this

$db_config = $config['db_engine'] . ":host=".$config['db_host'] . ";dbname=" . $config['db_name'].";charset=utf8mb4";

try {
    $pdo = new PDO($db_config, $config['db_user'], $config['db_password'], [
        PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"
    ]);

    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
} catch (PDOException $e) {
    exit("Impossibile connettersi al database: " . $e->getMessage());
}

Answered By – Leonardo Rosati

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published