UTF-8 Database Problem

前端 未结 3 527
终归单人心
终归单人心 2020-12-21 05:30

I\'ve a MySQL table that has a UTF-8 charset and upon attempting to insert to it via a PHP form, the database gives the following error:

PDOStatement:

3条回答
  •  醉话见心
    2020-12-21 06:23

    Your database might be set to UTF-8, but the database connection also needs to be set to UTF-8. You should do that with a SET NAMES utf8 statement. You can use the driver_options in PDO to have it execute that as soon as you connect:

    $handle = new PDO("mysql:host=localhost;dbname=dbname",
        'username', 'password', 
        array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
    

    Have a look at the following two links for more detailed information about making sure your entire site uses UTF-8 appropriately:

    • UTF-8 all the way through…
    • UTF8, PHP and MySQL

提交回复
热议问题