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

发布于 2024-07-13 17:49:10 字数 184 浏览 9 评论 0 原文

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

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

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?

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.

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

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

发布评论

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

评论(8

满地尘埃落定 2024-07-20 17:49:11

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

这是我今天在解决这个问题时学到的东西:

  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. 您还需要确保您有 '127.0.0.1 localhost ' 在你的 windows\system32\drivers\etc\hosts 隐藏系统文件中(这对于 php 5.3.0 来说不是问题)

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

All this methods shoud work, 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.

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)

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

时光是把杀猪刀 2024-07-20 17:49:11

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

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

完整示例

$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; 

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; 
笨笨の傻瓜 2024-07-20 17:49:11

在您的引导文件中...

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

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

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

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);
就是爱搞怪 2024-07-20 17:49:11
$table->getAdapter()->query('SET NAMES UTF8');
$table->getAdapter()->query('SET NAMES UTF8');
浪漫之都 2024-07-20 17:49:10

害怕我的google-fu

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

第一次点击;)

fear my 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 ;)

月寒剑心 2024-07-20 17:49:10

伊泰,

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

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

1002 是常量 PDO::MYSQL_ATTR_INIT_COMMAND 的值

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

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

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

阳光①夏 2024-07-20 17:49:10

只需将其放入您的配置中

database.params.charset = "utf8"

或在 ZF 1.11 之后这将起作用
resources.db.params.charset = utf8
这就对了

just put this in your config

database.params.charset = "utf8"

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

深白境迁sunset 2024-07-20 17:49:10

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

所以运行:

$db->query("设置名称'utf8'"); 
  

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

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.

so running:

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

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

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