PHP/MySQL中的PDO和UTF-8特殊字符? [英] PDO and UTF-8 special characters in PHP / MySQL?

查看:79
本文介绍了PHP/MySQL中的PDO和UTF-8特殊字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用MySQL和PHP 5.3,并尝试了此代码.

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

$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$con = mysql_connect("localhost", "root", "");
mysql_set_charset('utf8');
if (!$con)
{
  die('Could not connect: ' . mysql_error());
}

mysql_select_db("kdict", $con);
$sql = "SELECT * FROM `en-kh` where english='a'";
echo $sql;
$result = mysql_query($sql);

while($row = mysql_fetch_array($result))
{
  echo $row['english'] . " </br> " . $row['khmer'];
  echo "<br />";
}
?>

=>我的UTF-8渲染效果很好,做得很好.

=> I got good UTF-8 render display, well done.

但是现在,我创建了一个PDO类,以使其易于扩展并且更加容易

But for now I create a class PDO to keep easy to extend and more easy

 class crud {
     // code..
     public function conn()
     {
         isset($this->username);
         isset($this->password);
         if (!$this->db instanceof PDO)
         {
             $this->db = new PDO($this->dsn, $this->username, $this->password);
             $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
             $this->db->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, "SET NAMES 'utf8'");   
          }
      }
      /*more code here*/
}



/*** a new crud object ***/
$crud = new crud();
/*** The DSN ***/
$crud->dsn = "mysql:dbname=kdict;host=localhost";

/*** MySQL username and password ***/
$crud->username = 'root';
$crud->password = '';
/*** select all records from table ***/
$records = $crud->rawSelect("SELECT * FROM `en-kh` where english='a'");

/*** fetch only associative array of values ***/
$rows = $records->fetchAll(PDO::FETCH_ASSOC);

/*** display the records ***/
foreach($rows as $row)
{
    foreach($row as $fieldname=>$value)
    {
        echo $fieldname.' = '.$value.'<br />';
    }
    echo '<hr />';
}
?>

但是它显示了我的角色,例如'????'

But it displays my character something like this '????'

我在Stack Overflow上找到了此链接,看起来就像遇到了同样的问题 PHP/MySQL中的特殊字符

I found this link on Stack Overflow, it looks like the same problem i met Special characters in PHP / MySQL

它看起来与我的问题相同=>我试图修复它,但是我仍然无法正常工作.

It looks the same as my problem => I tried to fix it, but I still doesn't work.

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

谁能告诉我问题出在哪里?我该如何纠正?

Can anyone tell me what the problem is? How can I correct it?

谢谢

推荐答案

您缺少 S :

您当然也需要取消对其注释.此外,PDO::MYSQL_ATTR_INIT_COMMAND 不能不能设置

You also need to un-comment it of course. Also, PDO::MYSQL_ATTR_INIT_COMMAND can not be set with PDO::setAttribute() after you've established your database connection (the constant name says it all), you've to specify it in the constructor using the $driver_options argument, like this:

$this->db = new PDO($this->dsn, $this->username, $this->password, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'"));

一种替代方法是在连接后立即执行相同的查询:

An alternative to this is to just execute that very same query immediately after connecting:

$this->db = new PDO($this->dsn, $this->username, $this->password);
$this->db->exec("SET NAMES 'utf8';");

这篇关于PHP/MySQL中的PDO和UTF-8特殊字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆