search

Home  >  Q&A  >  body text

PDOException "Driver not found"

<p>I just installed Debian Lenny with Apache, MySQL and PHP and I get a PDOException <code>Unable to find driver</code>. </p> <p>This is the specific line of code it refers to: </p> <p><code>$dbh = new PDO('mysql:host=' .DB_HOST . ';dbname=' .DB_NAME, DB_USER, DB_PASS)</code></p> <p><code>DB_HOST</code>, <code>DB_NAME</code>, <code>DB_USER</code> and <code>DB_PASS</code> are constants I defined. It works fine on a production server (as well as my previous Ubuntu server setup). </p> <p>Is this related to my PHP installation? </p> <p>Searching online didn't help, all I got were expert talks and examples, but no solutions. </p>
P粉680000555P粉680000555487 days ago575

reply all(2)I'll reply

  • P粉242126786

    P粉2421267862023-08-24 13:03:53

    The dsn in the code shows that you are trying to connect to the mysql driver. Your error message indicates that the driver is not available.

    Check whether the mysql extension is installed on your server.

    In Ubuntu/Debian, you can check the package using the following command:

    dpkg --get-selections | grep php | grep mysql

    If there is no php5-mysql package, please install it.

    In Ubuntu/Debian you can use:

    • PHP5:sudo apt-get install php5-mysql
    • PHP7:sudo apt-get install php7.0-mysql

    Finally, to make it work, you need to restart the web server:

    • Apache: sudo /etc/init.d/apache2 restart
    • Nginx:sudo /etc/init.d/nginx restart

    reply
    0
  • P粉520545753

    P粉5205457532023-08-24 12:28:32

    You need to have a module called pdo_mysql. Look for the following in phpinfo(),

    pdo_mysql
    
    PDO Driver for MySQL, client library version => 5.1.44

    reply
    0
  • Cancelreply