-1

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;
  • check https://stackoverflow.com/questions/4361459/php-pdo-charset-set-names – Mate Aug 23 '22 at 17:06
  • This seems to working fine on my side. The problem maybe from the other parts that you did not post in the question, maybe your `dsn` or collation of the database itself. I recommend to use `utf8mb4_unicode_ci` instead to supported Chinese language, emoji icons and more new characters. – vee Aug 23 '22 at 17:31

1 Answers1

0

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());
}
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – jasie Sep 01 '22 at 09:01