php 在 ZendFramework 中,如何让 PDO 每次连接时都运行 SET NAMES utf8

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/584676/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:09:09  来源:igfitidea点击:

How to make PDO run SET NAMES utf8 each time I connect, In ZendFramework

phpzend-frameworkpdozend-db-table

提问by Itay Moav -Malimovka

How to make PDO adapter run SET NAMES utf8 each time I connect, In ZendFramework. I am using an INI file to save the adapter config data. what entries should I add there?

如何让 PDO 适配器每次连接时都运行 SET NAMES utf8,在 ZendFramework 中。我正在使用 INI 文件来保存适配器配置数据。我应该在那里添加哪些条目?

If it wasn't clear, I am looking for the correct syntax to do it in the config.ini file of my project and not in php code, as I regard this part of the configuration code.

如果不清楚,我正在我的项目的 config.ini 文件中寻找正确的语法,而不是在 php 代码中,因为我认为这部分是配置代码。

回答by SchizoDuckie

fear my google-fu

害怕我的google-fu

$pdo = new PDO(
    'mysql:host=mysql.example.com;dbname=example_db',
    "username",
    "password",
    array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));

first hit ;)

第一次命中 ;)

回答by David Snabel-Caunt

Itay,

伊泰,

A very good question. Fortunately for you the answer is very simple:

一个很好的问题。幸运的是,答案很简单:

database.params.driver_options.1002 = "SET NAMES utf8"

1002 is the value of constant PDO::MYSQL_ATTR_INIT_COMMAND

1002 是常量 PDO::MYSQL_ATTR_INIT_COMMAND 的值

You can't use the constant in the config.ini

您不能在 config.ini 中使用常量

回答by tawfekov

just put this in your config

把它放在你的配置中

database.params.charset = "utf8"

or after ZF 1.11 this would work to resources.db.params.charset = utf8that is it

或在 ZF 1.11 之后,这将起作用 resources.db.params.charset = utf8,就是这样

回答by Leon Fedotov

The connection in zend_db is lazy which means it connects on the first query. if you have a static page with no query's in it it will never even connect - even if it is initialized in you bootstrap file.

zend_db 中的连接是惰性的,这意味着它在第一个查询时连接。如果你有一个没有查询的静态页面,它甚至永远不会连接 - 即使它在你的引导文件中初始化。

so running:

所以运行:

$db->query("SET NAMES 'utf8'");
$db->query("SET NAMES 'utf8'");

Is not so smart. Big thanks to dcaunt for his solution.

不是那么聪明。非常感谢 dcaunt 的解决方案。

回答by mlarcher

All this methods shoudwork, except in some special circumstances. For example, if you are running a web server locally on a windows machine with php < 5.3.1, only a 'manual' $db->query("SET NAMES 'utf8'"); before your actual query will work. Any other method trying to use MYSQL_ATTR_INIT_COMMAND will fail.

所有这些方法都应该有效,除非在某些特殊情况下。例如,如果您在 php < 5.3.1 的 Windows 机器上本地运行 Web 服务器,则只需'手动' $db->query("SET NAMES 'utf8'"); 在您的实际查询起作用之前。任何其他尝试使用 MYSQL_ATTR_INIT_COMMAND 的方法都将失败。

Here's what I learnt today, struggling with this very problem :

这是我今天学到的东西,正在努力解决这个问题:

  1. You can't refer to PDO::MYSQL_ATTR_INIT_COMMAND in some environments (i.e. mine, specifically I don't know). You have to explicitely use 1002 instead

  2. With Zend Framework 1.11 ( perhaps since 1.8, to be confirmed ), you don't need to set database.params.driver_options.1002 = "SET NAMES utf8" in your config.ini : resources.db.params.charset = "utf8" will be enough for Zend_Db_Adapter_Pdo_Mysql to do it for you.

  3. On windows, you need php >= 5.3.1 for MYSQL_ATTR_INIT_COMMAND to work.

  4. If you replace your php version with 5.3.1 or higher (I also tested 5.3.3), you need to make sure you set a value to pdo_mysql.default_socket in your php.ini. The default empty value won't work (to be confirmed : I read something about this but didn't bother to try without it after finding out about point 5)

  5. You also need to make sure you have '127.0.0.1 localhost' in your windows\system32\drivers\etc\hosts hidden system file (that wasn't a problem for php 5.3.0)

  1. 您不能在某些环境中引用 PDO::MYSQL_ATTR_INIT_COMMAND(即我的,特别是我不知道)。您必须明确使用 1002 代替

  2. 使用 Zend Framework 1.11(可能从 1.8 开始,待确认),您不需要在 config.ini 中设置 database.params.driver_options.1002 = "SET NAMES utf8" :resources.db.params.charset = "utf8 " 足以让 Zend_Db_Adapter_Pdo_Mysql 为你做这件事。

  3. 在 Windows 上,您需要 php >= 5.3.1 才能使 MYSQL_ATTR_INIT_COMMAND 工作。

  4. 如果您将 php 版本替换为 5.3.1 或更高版本(我也测试了 5.3.3),则需要确保在 php.ini 中为 pdo_mysql.default_socket 设置了一个值。默认的空值不起作用(有待确认:我阅读了一些关于此的内容,但在发现第 5 点后并没有费心尝试没有它)

  5. 您还需要确保您的 windows\system32\drivers\etc\hosts 隐藏系统文件中有“127.0.0.1 localhost”(这对 php 5.3.0 来说不是问题)

With all this in mind, you should be able to save from yourself a day of googling and keep some of your hairs! ;)

考虑到所有这些,您应该能够节省一天的谷歌搜索时间并保留一些头发!;)

回答by Black

You just have to execute this command before you start queries, you only have to execute it once before the queries and not for every query.

您只需要在开始查询之前执行此命令,您只需在查询之前执行一次,而不是对每个查询执行一次。

$pdo->query("SET NAMES 'utf8'");

Full example

完整示例

$servername = "localhost";
$username = "root";
$password = "test";
$dbname = "yourDB";

try {
    $pdo = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);

    $pdo->query("SET NAMES 'utf8'");

    //set the PDO error mode to exception
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $sql = "SELECT name FROM nations";
    foreach ($pdo->query($sql) as $row) {
       echo "<option value='".$row['name']."'>".$row['name']."</option>";
    }


} catch(PDOException $e) {
    echo "Error: " . $e->getMessage();
}
$pdo = null; 

回答by Black

In your bootstrap file...

在您的引导文件中...

$db = Zend_Db::factory($adapter, $config);
$db->query("SET NAMES 'utf8'");

then you save this instance in your registry

然后您将此实例保存在您的注册表中

Zend_Registry::set('db', $db);

回答by wormhit

$table->getAdapter()->query('SET NAMES UTF8');