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

时间:2023-01-06 14:42:21

This code I were try .I am using Mysql and php 5.3

这个代码我试过。我正在使用Mysql和php 5.3

$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 />";
  }
  ?>

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

=>我得到了很好的utf-8渲染显示,做得很好。

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

但是现在我创建了一个类PDO,以便于扩展和更容易

 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 display with my character something like this '????'

但它显示我的角色像这样'????'

I found in * this link it look the same problem i met Special characters in PHP / MySQL

我发现在*这个链接它看起来同样的问题我遇到PHP / MySQL中的特殊字符

It look the same my problem => I try to fixed it .but I still not got it. $this->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, "SET NAME'utf8'");

看起来我的问题一样=>我试着修理它。但我还是没有得到它。 $ this-> setAttribute(PDO :: MYSQL_ATTR_INIT_COMMAND,“SET NAME'utf8'”);

Anyone can told me what is problem?How Can I correct it?

任何人都可以告诉我什么是问题?我该如何纠正?

thanks

谢谢

3 个解决方案

#1


34  

You're missing an S: it's SET NAMES and not SET NAME:

你错过了一个S:它是SET NAMES而不是SET NAME:

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

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:

当然,您还需要取消评论。此外,在建立数据库连接(常量名称表示全部)后,无法使用PDO :: setAttribute()设置PDO :: MYSQL_ATTR_INIT_COMMAND,您必须使用$ driver_options参数在构造函数中指定它,如这个:

$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';");

#2


2  

I had the same trouble and found out after trying out 1000000 different options that the default mysql engine still remains MyISAM.

我遇到了同样的麻烦,并在尝试了1000000个不同的选项后发现默认的mysql引擎仍然是MyISAM。

Verifying that InnoDB is the Default Storage Engine as such:

验证InnoDB是默认存储引擎,如下所示:

Issue the command SHOW VARIABLES LIKE 'have_innodb'; to confirm that InnoDB is available at all.

发出命令SHOW VARIABLES LIKE'has_innodb';确认InnoDB完全可用。

Then issue the command SHOW ENGINES; to see all the different MySQL storage engines.

然后发出命令SHOW ENGINES;查看所有不同的MySQL存储引擎。

Look for DEFAULT in the InnoDB line and NOT in the MyISAM line. I realized that my provided had a setting preventing me from changing the default engine using SET storage_engine=MYISAM;. In my case, I contacted my provider and was directed to where I could change the option to be able to change the default engine. Hope this helps!

在InnoDB行中查找DEFAULT而不在MyISAM行中查找。我意识到我提供的设置阻止我使用SET storage_engine = MYISAM;更改默认引擎。在我的情况下,我联系了我的提供商,并被指示我可以更改选项以便能够更改默认引擎。希望这可以帮助!

#3


0  

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

使用PDO插入特殊字符的可能方法,只需按照以下步骤操作:

1) Set the Attribute to you connection class.

1)将Attribute设置为您的连接类。

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

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

2)现在,您可以在创建查询和插入数据库时​​使用htmlspecialchars:

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

And it will work fine.

它会工作正常。

#1


34  

You're missing an S: it's SET NAMES and not SET NAME:

你错过了一个S:它是SET NAMES而不是SET NAME:

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

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:

当然,您还需要取消评论。此外,在建立数据库连接(常量名称表示全部)后,无法使用PDO :: setAttribute()设置PDO :: MYSQL_ATTR_INIT_COMMAND,您必须使用$ driver_options参数在构造函数中指定它,如这个:

$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';");

#2


2  

I had the same trouble and found out after trying out 1000000 different options that the default mysql engine still remains MyISAM.

我遇到了同样的麻烦,并在尝试了1000000个不同的选项后发现默认的mysql引擎仍然是MyISAM。

Verifying that InnoDB is the Default Storage Engine as such:

验证InnoDB是默认存储引擎,如下所示:

Issue the command SHOW VARIABLES LIKE 'have_innodb'; to confirm that InnoDB is available at all.

发出命令SHOW VARIABLES LIKE'has_innodb';确认InnoDB完全可用。

Then issue the command SHOW ENGINES; to see all the different MySQL storage engines.

然后发出命令SHOW ENGINES;查看所有不同的MySQL存储引擎。

Look for DEFAULT in the InnoDB line and NOT in the MyISAM line. I realized that my provided had a setting preventing me from changing the default engine using SET storage_engine=MYISAM;. In my case, I contacted my provider and was directed to where I could change the option to be able to change the default engine. Hope this helps!

在InnoDB行中查找DEFAULT而不在MyISAM行中查找。我意识到我提供的设置阻止我使用SET storage_engine = MYISAM;更改默认引擎。在我的情况下,我联系了我的提供商,并被指示我可以更改选项以便能够更改默认引擎。希望这可以帮助!

#3


0  

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

使用PDO插入特殊字符的可能方法,只需按照以下步骤操作:

1) Set the Attribute to you connection class.

1)将Attribute设置为您的连接类。

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

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

2)现在,您可以在创建查询和插入数据库时​​使用htmlspecialchars:

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

And it will work fine.

它会工作正常。