PDO连接:使用SET NAMES / CHARACTER SET的UTF-8声明? [英] PDO connection : UTF-8 declaration with SET NAMES / CHARACTER SET?

查看:513
本文介绍了PDO连接:使用SET NAMES / CHARACTER SET的UTF-8声明?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据php.net,StackOverflow和其他信任来源,我可以找到4种不同的方式在PDO连接上设置UTF-8,但是找不到最好的选择。



PDO连接代码(以及一些init):

  $ localhost = $ _SERVER [ SERVER_NAME'] =='localhost'; 
error_reporting(-1); ini_set('display_errors',$ localhost); // Old:error_reporting($ localhost?-1:0);请参阅上面的回答
date_default_timezone_set('Europe / Paris');

$ pdo_db ='mysql:host = localhost; dbname = local_db; charset = utf8'; // METHOD#1
$ pdo_login ='root';
$ pdo_pass ='localpass';

try {
$ db = new PDO($ pdo_db,$ pdo_login,$ pdo_pass,array(
PDO :: ATTR_ERRMODE => $ localhost?PDO :: ERRMODE_EXCEPTION: PDO :: ERRMODE_SILENT,
PDO :: MYSQL_ATTR_INIT_COMMAND =>'SET NAMES utf8',//方法#2
));
$ db - > exec('SET NAMES utf8'); // METHOD#3
$ db - > exec('SET CHARACTER SET utf8'); // METHOD#4
$ db - > exec('SET time_zone = \''。date_default_timezone_get()。'\'');
} catch(PDOException $ error){
die($ error - > getMessage());
}

所以,我的理解是,方法1只适用于PHP 5.3 +(但似乎它有点儿错误),方法2是仅为MySQL。
方法3和4之间的区别是 MySQL东西,但是我仍然不知道一个更好。有没有办法在PDO属性中调用SET NAMES,但不能用于MySQL?



谢谢!

解决方案

在DSN中设置是 only 正确的方式(虽然它只在5.3以后才支持)。

你可以这个和 SET NAMES



所有其他方式将使臭名昭着的半虚拟GBK注入成为可能。 p>

请注意,error_reporting()的设置是完全错误的。它必须是无条件的-1。
如果你担心显示错误 - 有一个适当的ini设置为 display_errors ,可以在运行时设置。 br>
虽然error_reporting设置级别的错误,应该始终为最大值。


According to php.net, StackOverflow and other sources of trust, I can find 4 different ways to set UTF-8 on PDO connection, but can't find wich one is the better to choose.

A PDO connection code (and some inits) :

$localhost = $_SERVER['SERVER_NAME'] == 'localhost';
error_reporting(-1); ini_set('display_errors', $localhost); // Old : error_reporting($localhost ? -1 : 0); see answer above
date_default_timezone_set('Europe/Paris');

$pdo_db = 'mysql:host=localhost;dbname=local_db;charset=utf8'; // METHOD #1
$pdo_login = 'root';
$pdo_pass = 'localpass';

try {
    $db = new PDO($pdo_db, $pdo_login, $pdo_pass, array(
        PDO::ATTR_ERRMODE => $localhost ? PDO::ERRMODE_EXCEPTION : PDO::ERRMODE_SILENT,
        PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8', // METHOD #2
    ));
    $db -> exec('SET NAMES utf8'); // METHOD #3
    $db -> exec('SET CHARACTER SET utf8'); // METHOD #4
    $db -> exec('SET time_zone = \''.date_default_timezone_get().'\'');
} catch (PDOException $error) {
    die($error -> getMessage());
}

So, what I understood, is that the Method 1 only work with PHP 5.3+ (but it seems that it's a bit buggy), and method 2 is for MySQL only. Differences between method 3 and 4 is MySQL thing, but I still don't know wich one is better. And is there a way to call SET NAMES in PDO attributes, but not for MySQL only?

Thanks!

解决方案

Setting it in DSN is the only proper way (although it is only supported since 5.3).
You can this one and SET NAMES at the same time.

All the other ways will make infamous half-fictional GBK injection possible.

Please note that your setting for error_reporting() is utterly wrong. it have to be unconditional -1. If you concerned about displaying errors - there is a proper ini setting for this, called display_errors, can be set at runtime.
While error_reporting sets level of the error and should be at max all the time.

这篇关于PDO连接:使用SET NAMES / CHARACTER SET的UTF-8声明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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