<tfoot id='TrD4f'></tfoot>
    • <bdo id='TrD4f'></bdo><ul id='TrD4f'></ul>

    <legend id='TrD4f'><style id='TrD4f'><dir id='TrD4f'><q id='TrD4f'></q></dir></style></legend>
  • <small id='TrD4f'></small><noframes id='TrD4f'>

        <i id='TrD4f'><tr id='TrD4f'><dt id='TrD4f'><q id='TrD4f'><span id='TrD4f'><b id='TrD4f'><form id='TrD4f'><ins id='TrD4f'></ins><ul id='TrD4f'></ul><sub id='TrD4f'></sub></form><legend id='TrD4f'></legend><bdo id='TrD4f'><pre id='TrD4f'><center id='TrD4f'></center></pre></bdo></b><th id='TrD4f'></th></span></q></dt></tr></i><div id='TrD4f'><tfoot id='TrD4f'></tfoot><dl id='TrD4f'><fieldset id='TrD4f'></fieldset></dl></div>

        PHP/MySQL 中的 PDO 和 UTF-8 特殊字符?

        时间:2023-10-05

          <bdo id='YphJW'></bdo><ul id='YphJW'></ul>

              <tbody id='YphJW'></tbody>
              <legend id='YphJW'><style id='YphJW'><dir id='YphJW'><q id='YphJW'></q></dir></style></legend>

                  <small id='YphJW'></small><noframes id='YphJW'>

                  <tfoot id='YphJW'></tfoot>
                  <i id='YphJW'><tr id='YphJW'><dt id='YphJW'><q id='YphJW'><span id='YphJW'><b id='YphJW'><form id='YphJW'><ins id='YphJW'></ins><ul id='YphJW'></ul><sub id='YphJW'></sub></form><legend id='YphJW'></legend><bdo id='YphJW'><pre id='YphJW'><center id='YphJW'></center></pre></bdo></b><th id='YphJW'></th></span></q></dt></tr></i><div id='YphJW'><tfoot id='YphJW'></tfoot><dl id='YphJW'><fieldset id='YphJW'></fieldset></dl></div>
                  本文介绍了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:它是 SET NAMES 而不是 SET NAME:

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

                  你当然也需要取消注释.另外,PDO::MYSQL_ATTR_INIT_COMMAND 可以设置与PDO::setAttribute() 建立数据库连接后(常量名说明了一切),您必须指定它在 constructor 中使用 $driver_options 参数,例如这个:

                  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 特殊字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:清理和验证表单 php 下一篇:我如何 ping MySQL 数据库并使用 PDO 重新连接

                  相关文章

                  最新文章

                    • <bdo id='oD537'></bdo><ul id='oD537'></ul>
                  1. <small id='oD537'></small><noframes id='oD537'>

                    <i id='oD537'><tr id='oD537'><dt id='oD537'><q id='oD537'><span id='oD537'><b id='oD537'><form id='oD537'><ins id='oD537'></ins><ul id='oD537'></ul><sub id='oD537'></sub></form><legend id='oD537'></legend><bdo id='oD537'><pre id='oD537'><center id='oD537'></center></pre></bdo></b><th id='oD537'></th></span></q></dt></tr></i><div id='oD537'><tfoot id='oD537'></tfoot><dl id='oD537'><fieldset id='oD537'></fieldset></dl></div>
                      <legend id='oD537'><style id='oD537'><dir id='oD537'><q id='oD537'></q></dir></style></legend>
                      <tfoot id='oD537'></tfoot>