php 带有 pdo 的未定义类常量“MYSQL_ATTR_INIT_COMMAND”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2424343/
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
Undefined class constant 'MYSQL_ATTR_INIT_COMMAND' with pdo
提问by user260019
$db = new PDO('mysql:dbname=xnews;host=localhost;port=' . $LOCAL_DB_PORT,
$LOCAL_DB_USER,
$LOCAL_DB_PASS,
array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'UTF8'")
);
reports:
报告:
Undefined class constant 'MYSQL_ATTR_INIT_COMMAND'
未定义的类常量 'MYSQL_ATTR_INIT_COMMAND'
Is it renamed?
改名了吗?
回答by Sk8erPeter
I just had the same error (with PHP 5.2.6), and all I had to do is enable the MySQL-specific PDO driver:
我刚刚遇到了同样的错误(使用 PHP 5.2.6),我所要做的就是启用特定于 MySQL 的 PDO 驱动程序:
TL;DR
TL; 博士
In your php.inifile, you should have the following line (uncommented):
在您的php.ini文件中,您应该有以下行(未注释):
extension=php_pdo_mysql.dllon Windowsextension=php_pdo_mysql.soon Linux/Mac
extension=php_pdo_mysql.dll在 Windows 上extension=php_pdo_mysql.so在 Linux/Mac 上
Longer explanation:
更长的解释:
open
php.iniin a text editor- e.g. its default path on Windows:
C:\Program Files (x86)\PHP\v5.X\php.ini(substitute v5.x with the version you installed) orC:\Windows\php.ini, etc. - or on Linux:
/etc/php5/apache2/php.ini(e.g. this is the path on Ubuntu) or/etc/php5/cli/php.ini,/etc/php5/cgi/php.ini, etc. - or you can get to know where it is like this:
php --ini | find /i "Loaded"in Windows command prompt ORphp --ini | grep "Loaded"in Linux/Mac terminal- using
phpinfo(), and looking for the line "Loaded Configuration File"
- e.g. its default path on Windows:
and remove the semicolon from the beginning of the following line(to uncomment it):
;extension=php_pdo_mysql.dllon Windows- OR
;extension=php_pdo_mysql.soon Linux/Mac
- OR
- of course, if this line doesn't exist, you should paste it
- so as a resultthe expected line would look like this in
php.ini:extension=php_pdo_mysql.dllon Windows- OR
extension=php_pdo_mysql.soon Linux/Mac
You may need to restart your web server.
php.ini在文本编辑器中打开- 例如,它在 Windows 上的默认路径:(
C:\Program Files (x86)\PHP\v5.X\php.ini用您安装的版本替换 v5.x)或C:\Windows\php.ini等。 - 或Linux上:
/etc/php5/apache2/php.ini(例如,这是在Ubuntu路径)或/etc/php5/cli/php.ini,/etc/php5/cgi/php.ini等 - 或者你可以知道它在哪里:
php --ini | find /i "Loaded"在 Windows 命令提示符下或php --ini | grep "Loaded"在 Linux/Mac 终端- 使用
phpinfo(), 并寻找“加载的配置文件”行
- 例如,它在 Windows 上的默认路径:(
并从以下行的开头删除分号(取消注释):
;extension=php_pdo_mysql.dll在 Windows 上- 或
;extension=php_pdo_mysql.so在 Linux/Mac 上
- 或
- 当然,如果这条线不存在,你应该粘贴它
- 所以作为结果的预期路线是这样的
php.ini:extension=php_pdo_mysql.dll在 Windows 上- 或
extension=php_pdo_mysql.so在 Linux/Mac 上
您可能需要重新启动 Web 服务器。
That solved my problem.
那解决了我的问题。
回答by Sverre
I got the same error, on debian6, when I had not yet installed php5-mysql.
我在 debian6 上遇到了同样的错误,当时我还没有安装php5-mysql.
So I installed it, then restarted apache2
所以我安装了它,然后重新启动了apache2
apt-get install php5-mysql
/etc/init.d/apache2 restart
Then the error went away.
然后错误消失了。
If you have the same error on Ubuntu, instead of:
如果您在 Ubuntu 上遇到相同的错误,而不是:
/etc/init.d/apache2 restart
Type:
类型:
service apache2 restart
回答by Atli
It appears to only be availabe using the mysqlnddriver.
Try replacing it with the integer it represents; 1002, if I am not mistaken.
它似乎只能使用mysqlnd驱动程序。
尝试用它代表的整数替换它;1002,如果我没记错的话。
回答by Aris
For Centos I was missing php-mysql library:
对于 Centos,我缺少 php-mysql 库:
yum install php-mysql
service httpd restart
There is no need to enable any extension in php.ini, it is loaded by default.
php.ini 中无需启用任何扩展,默认加载。
回答by Pascal MARTIN
I just tried with PHP 5.2, and that constant seems to exists :
我刚刚尝试使用 PHP 5.2,并且该常量似乎存在:
var_dump(PDO::MYSQL_ATTR_INIT_COMMAND);
Gives me :
给我 :
int 1002
But it seems there is a bug in PHP 5.3, that causes this constant to not exists anymore -- or, at least, not when the mysqlnd driver is used (and it's the one that's configured by default)
但似乎 PHP 5.3 中存在一个错误,导致此常量不再存在——或者,至少,在使用 mysqlnd 驱动程序时不存在(并且它是默认配置的驱动程序)
I suppose a temporary solution, as suggested on this bug report, could be to directly use the integer 1002value, instead of the contant...
我想一个临时解决方案,如this bug report中所建议的,可能是直接使用整1002数值,而不是常量...
But note that you should go back to using the constant as soon as possible -- as this makes the code easier to understand.
但请注意,您应该尽快返回使用常量——因为这会使代码更易于理解。
回答by Reza S
For me it was missing MySQL PDO, I recompiled my PHP with the --with-pdo-mysqloption, installed it and restarted apache and it was all working
对我来说,它缺少 MySQL PDO,我使用该--with-pdo-mysql选项重新编译了我的 PHP ,安装了它并重新启动了 apache,一切正常
回答by Andrew
You could try replacing it with 1002 or issuing your initialization query right after opening a connection.
您可以尝试将其替换为 1002 或在打开连接后立即发出初始化查询。
回答by Jeff Busby
I got this error this morning, I just did a fresh install of Fedora 14 and was trying to get my local projects back online. I was missing php-mysql, I installed it via yum and the error is now gone.
今天早上我遇到了这个错误,我刚刚重新安装了 Fedora 14,并试图让我的本地项目重新上线。我错过了 php-mysql,我通过 yum 安装了它,现在错误消失了。
回答by Siraj Khan
This is due to a PHP 5.3.0 bug on Windows where MYSQL_ATTR_INIT_COMMAND is not available. The PHP bug report is:
这是由于 Windows 上的 PHP 5.3.0 错误导致 MYSQL_ATTR_INIT_COMMAND 不可用。PHP错误报告是:
http://bugs.php.net/bug.php?id=47224
http://bugs.php.net/bug.php?id=47224
If you are experiencing this, please update your WAMP product to a version that uses PHP 5.3.1 or later version.
如果您遇到这种情况,请将您的 WAMP 产品更新到使用 PHP 5.3.1 或更高版本的版本。
回答by JDelage
Using the int value 1002seems to work for PHP 5.3.0:
使用 int 值1002似乎适用于 PHP 5.3.0:
public static function createDB() {
$dbHost="localhost";
$dbName="project";
$dbUser="admin";
$dbPassword="whatever";
$dbOptions=array(1002 => 'SET NAMES utf8',);
return new DB($dbHost, $dbName, $dbUser, $dbPassword,$dbOptions);
}
function createConnexion() {
return new PDO(
"mysql:host=$this->dbHost;dbname=$this->dbName",
$this->dbUser,
$this->dbPassword,
$this->dbOptions);
}

