Undefined class constant 'MYSQL_ATTR_INIT_COMMAND' with pdo

后端 未结 11 2097
暖寄归人
暖寄归人 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:37

    I got the same error, on debian6, when I had not yet installed php5-mysql.

    So I installed it, then restarted 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:

    /etc/init.d/apache2 restart  
    

    Type:

    service apache2 restart
    
    0 讨论(0)
  • 2020-11-29 07:37

    For me it was missing MySQL PDO, I recompiled my PHP with the --with-pdo-mysql option, installed it and restarted apache and it was all working

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

    It appears to only be availabe using the mysqlnd driver.
    Try replacing it with the integer it represents; 1002, if I am not mistaken.

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

    You could try replacing it with 1002 or issuing your initialization query right after opening a connection.

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

    For anyone coming along later, like I did, and seeing this...

    I was receiving the same error because I did not have the extension "enabled" in my php.ini file. I would imagine you might also get this error if you have recently upgraded your php version and did not properly update your php.ini file.

    If you are receiving this error shortly after upgrading your php version, the info below might help you out:

    PHP 7.4 slightly changed its syntax in the php.ini file. Now, to enable the mysql pdo, make sure extension=pdo_mysql is uncommented in your php.ini file. (line 931 in the default php.ini setup)

    The line used to be:

    extension=php_pdo_mysql.dll on Windows

    extension=php_pdo_mysql.so on Linux/Mac

    as Sk8erPeter pointed out. but the .dll and .so endings are to be deprecated and so it is best practice to begin getting rid of those endings and using just extension=<ext>

    The below is pulled from the default php.ini-production file from the php 7.4 zip download under "Dynamic Extensions":

    Note : The syntax used in previous PHP versions ('extension=.so' and 'extension='php_.dll') is supported for legacy reasons and may be deprecated in a future PHP major version. So, when it is possible, please move to the new (extension=<ext>) syntax.

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

    I just had the same error (with PHP 5.2.6), and all I had to do is enable the MySQL-specific PDO driver:

    TL;DR

    In your php.ini file, you should have the following line (uncommented):

    • extension=php_pdo_mysql.dll on Windows
    • extension=php_pdo_mysql.so on Linux/Mac

    Longer explanation:

    1. open php.ini in 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) or C:\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 OR
        • php --ini | grep "Loaded" in Linux/Mac terminal
        • using phpinfo(), and looking for the line "Loaded Configuration File"
    2. and remove the semicolon from the beginning of the following line (to uncomment it):

      • ;extension=php_pdo_mysql.dll on Windows
        • OR ;extension=php_pdo_mysql.so on Linux/Mac
      • of course, if this line doesn't exist, you should paste it
      • so as a result the expected line would look like this in php.ini:
        • extension=php_pdo_mysql.dll on Windows
        • OR extension=php_pdo_mysql.so on Linux/Mac
    3. You may need to restart your web server.

    That solved my problem.

    0 讨论(0)
提交回复
热议问题