Zend DB 和编码

发布于 2024-09-09 01:17:14 字数 404 浏览 8 评论 0原文

我刚刚遇到了一些相当奇怪的事情,我使用 Zend Framework 1.10 和 Zend_Db_Table 模块从数据库读取一些数据。数据库本身、表和相关字段都将其排序规则设置为“utf8_general_ci”,并且当使用 phpMyAdmin 检查时,所有特殊字符在数据库中显示格式正确。另外,使用 Zend_Db_Table 保存效果很好,但是当我读取数据并将其回显到浏览器时,它会以 ISO-8859-1 的形式返回,而不是以 UTF8 的形式返回。当尝试对从数据库返回的值使用 json_encode (仅适用于 UTF8 字符串作为输入)时,我注意到同样的事情。

我如何设置 Zend_Db_Table/Zend_Db_Row 应该始终使用 UTF8 并返回一个 UTF8 值?我还没有在我的应用程序中设置任何有关编码的内容。

非常感谢您的帮助!

I have just encountered something rather strange, I use the Zend Framework 1.10 with the Zend_Db_Table module to read some data from a databse. The database itself, the table and the fields in question all have their collation set to "utf8_general_ci" and all special chars appear correctly formatted in the DB when checked with phpMyAdmin. Also, saving with Zend_Db_Table works just fine, yet when I read the data and just echo it to my browser it is returned as ISO-8859-1, not as UTF8. I noticed the same thing when trying to use json_encode (which only works with UTF8 strings as input) on a value returned from the DB.

How can I set that Zend_Db_Table/Zend_Db_Row should always work with UTF8 and return me an UTF8 value? I have not set anything regarding encoding in my app yet.

Thanks a lot for your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

遮云壑 2024-09-16 01:17:14

请注意。就我而言,这有帮助:

    $this->db = new Zend_Db_Adapter_Pdo_Mysql(array(
        'host'     => $config['db_hostname'],
        'username' => $config['db_username'],
        'password' => $config['db_password'],
        'dbname'   => $config['db_database'],
        'charset'  => 'utf8'
    ));

Just note. In my case this one helped:

    $this->db = new Zend_Db_Adapter_Pdo_Mysql(array(
        'host'     => $config['db_hostname'],
        'username' => $config['db_username'],
        'password' => $config['db_password'],
        'dbname'   => $config['db_database'],
        'charset'  => 'utf8'
    ));
━╋う一瞬間旳綻放 2024-09-16 01:17:14
resources.db.params.charset = utf8

就像罗伯特·贝西克说的。

resources.db.params.charset = utf8

like robertbasic said.

柠檬心 2024-09-16 01:17:14

好吧,刚刚找到了解决方案,尝试这样做:

$db = Zend_Db::factory($config->database); // Setting up the DB
$db->query("SET NAMES 'utf8';");           // That's the magic line I was missing

希望这可以在某个时候帮助其他人:)

Ok just found the solution, try to do this:

$db = Zend_Db::factory($config->database); // Setting up the DB
$db->query("SET NAMES 'utf8';");           // That's the magic line I was missing

Hope this helps somebody else at some point :)

划一舟意中人 2024-09-16 01:17:14

您也可以将具有所需值的“charset”键放入配置中,DB驱动程序将执行适当的查询(取决于使用的DBMS)。看来目前(版本 1.10.5)几乎所有驱动程序都支持这一点。

Also you can just put "charset" key with desired value into config and DB-driver will execute an appropriate query (depending on DBMS used). It seems to be that currently (version 1.10.5) almost all drivers support that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文