PDO and UTF-8 special characters in PHP / MySQL?

前端 未结 4 802
北海茫月
北海茫月 2020-12-11 15:58

I am using MySQL and PHP 5.3 and tried this code.

$dbhost = \'localhost\';
$dbuser = \'root\';
$dbpass = \'\';
$con = mysql_connect(\"localhost\", \"root\",          


        
4条回答
  •  旧时难觅i
    2020-12-11 16:30

    The possible way to insert Special characters using PDO, just follow the these to steps:

    1) Set the Attribute to you connection class.

    $this->db->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, "SET NAME'utf8'");
    

    2) Now you can use htmlspecialchars while query creation and insertion to database:

    $ShortDescription   = htmlspecialchars($_POST['ShortDescription'], ENT_QUOTES);
    $LongDescription    = htmlspecialchars($_POST['LongDescription'],ENT_QUOTES);
    

    And it will work fine.

提交回复
热议问题