Undefined class constant 'MYSQL_ATTR_INIT_COMMAND' with pdo

后端 未结 11 2098
暖寄归人
暖寄归人 2020-11-29 07:27
$db = new PDO(\'mysql:dbname=xnews;host=localhost;port=\' . $LOCAL_DB_PORT, 
          $LOCAL_DB_USER, 
          $LOCAL_DB_PASS, 
          array(PDO::MYSQL_ATTR_IN         


        
相关标签:
11条回答
  • 2020-11-29 07:50

    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.

    0 讨论(0)
  • 2020-11-29 07:56

    For Centos I was missing php-mysql library:

    yum install php-mysql
    
    service httpd restart
    

    There is no need to enable any extension in php.ini, it is loaded by default.

    0 讨论(0)
  • 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:

    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.

    0 讨论(0)
  • 2020-11-29 07:59

    I just tried with PHP 5.2, and that constant seems to exists :

    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)

    I suppose a temporary solution, as suggested on this bug report, could be to directly use the integer 1002 value, instead of the contant...

    But note that you should go back to using the constant as soon as possible -- as this makes the code easier to understand.

    0 讨论(0)
  • 2020-11-29 08:00

    Using the int value 1002 seems to work for 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); 
    }
    
    0 讨论(0)
提交回复
热议问题