PDO和Mysql charset(PDO and Mysql charset)

编程入门 行业动态 更新时间:2024-10-27 20:39:18
PDO和Mysql charset(PDO and Mysql charset)

我最近切换到许多项目的PDO(使用mysql / i驱动程序太多时间被告知)并且遇到了PDO Mysql驱动程序和charset的奇怪行为。 我在StackOverflow上发现,使用pdo和mysql初始化utf8连接的正确方法是在DSN末尾添加Charset=utf8 。 然而,这个参数在5.3.6之前的php版本中被默默忽略。 因为我正在开发一个cms,我不知道它会运行哪个环境; 我正在寻找一种解决方案,使其兼容。

我找到的第一个答案是使用$PDO->exec("SET NAMES utf8"); 发出一个查询是不一样的,所以,它会使转义函数陷入困境。

第二个答案是将PDO::MYSQL_ATTR_INIT_COMMAND SET NAMES utf8为SET NAMES utf8 ,那么这与以前不一样吗? (等断开逃脱功能?)

其实我对此有点困惑,于是我决定寻求帮助。 我还想到了两个解决方案:

1)构建一个抽象层,在PHP <5.3.6的情况下,使用mysqli驱动程序代替

2)每次数据通过套接字时,使用另一个字符集并进行编码/解码

谢谢

I recently switched to PDO for many projects (used mysql/i drivers for too much time to be told) and encountered a strange behavior with the PDO Mysql driver and the charset. I found out on StackOverflow that THE correct way to intialize a utf8 connection using pdo and mysql is to add Charset=utf8 at the end of the DSN. Yet this parameter was silently ignored in php version prior to 5.3.6. Since I'm developing a cms, and I won't know which environment it will run on; I'm searching for a solution to make it compatible.

The first answer I found is use $PDO->exec("SET NAMES utf8"); Wouldn't that be the same of issuing a query, so, it would mess up thing with the escape functions.

The second answer is to set PDO::MYSQL_ATTR_INIT_COMMAND to SET NAMES utf8, so wouldn't that be the same as before ? (and so break escape functions ?)

Actually I'm getting a bit confused about this, and so I decided to ask for help. I've also thought of two solutions:

1) Build an abstraction layer and in case of PHP < 5.3.6 use mysqli driver instead

2) Utilize another charset and encode/decode each time the data are passed through the socket

Thanks

最满意答案

只是不要打扰。

事实上, utf-8使用与默认latin1 完全相同的转义规则。

所以,它不会搞乱逃生功能。 更不用说你根本不应该手动使用它们(尽管它们在仿真模式下以静默方式使用)。

只要您在网站上使用utf-8,只需在DSN中保留charset并输入$PDO->exec("SET NAMES utf8"); 为了兼容性,您将涵盖所有可能的案例。

“弄乱转义函数”的另一个问题是你可以关闭仿真模式 ,因此,只要你使用占位符来表示查询中的数据,

$conn->setAttribute( PDO::ATTR_EMULATE_PREPARES, false );

永远不会涉及逃避。 - 所以,根本不用担心。

Just don't bother.

As a matter of fact, utf-8 use exactly the same escaping rules as default latin1.

So, it wouldn't mess up thing with the escape functions. Not to mention that you shouldn't use them manually at all (though they are used silently in emulation mode).

As long as you are using utf-8 for your sites, just keep charset in DSN and put $PDO->exec("SET NAMES utf8"); for compatibility and you will have all the possible cases covered.

Another thing on "messing with escape functions" is that you can turn emulation mode off, and thus, as long as you are using placeholders to represent your data in the query,

$conn->setAttribute( PDO::ATTR_EMULATE_PREPARES, false );

no escaping would be involved ever. - so, there would be no point to worry at all.

更多推荐

本文发布于:2023-04-29 05:10:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1335281.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:PDO   Mysql   charset

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!