PHP Fatal error: Uncaught PDOException: could not find driver

前端 未结 4 659
暗喜
暗喜 2021-01-17 22:24

I have been trying to connect to MySQL from PHP using PDO. However, I get this error message:

PHP Fatal error: Uncaught PDOException: could not find

相关标签:
4条回答
  • 2021-01-17 22:52

    In my PHP 7.4 I didn't get php.ini. Instead, I have php.ini-development and php.ini-production. So, I have created a new php.ini file only and copied the configurations into it.

    Then I have uncommented and changed the extension directory to the full installation path.

     extension_dir = "C:/php/ext"
    

    And uncommented:

     extension=pdo_mysql
    
    0 讨论(0)
  • 2021-01-17 23:01

    Ok, somehow I had a problem with the extension itself. It helped to reinstall the MySQL extension. Here an example for PHP 7.3:

    sudo apt purge php7.3-mysql
    sudo apt install php7.3-mysql
    sudo service apache2 restart
    
    0 讨论(0)
  • 2021-01-17 23:06

    To use different drivers you need to install them. On Windows you simply uncomment a line in php.ini:

    extension=php_pdo_mysql.dll
    

    On Linux you install the extension with the package manager:

    sudo apt install php7.1-mysql
    
    0 讨论(0)
  • 2021-01-17 23:10

    I had the same problem, resulting from some incompatibility (not immediately evident) between the Apache and PHP versions I had downloaded. Try writing a toy PHP script that simply creates a new PDO object, - something like:

    <?php
    
    $dbname   = 'mydb';
    $username = 'myuser';
    $password = 'mypassword';
    try {
        $pdo = new \PDO("mysql:host=localhost;dbname=$dbname", $username,  $password);
    } catch (Exception $e) {
        print $e->getMessage() . "\n";
    }
    print "OK\n";
    

    Then run that script from the command line. If you don't get the 'could not find driver' error message, then that points to an incompatibility between your PHP and Apache versions.

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