skip to Main Content

Okay, so. I have PDO connection on MySQL database on local server. This is the code for it

<?php
    $servername = "localhost";
    $username = "root";
    $password = "";

try {
    $dbh = new PDO('localhost:host=$servername;dbname=test', $username, $password);
    $dbh = null;
} catch (PDOException $e) {
    print "Error!: " . $e->getMessage() . "<br/>";
    die();
}
?>

When I open the page it gives me this error.

Error!: could not find a driver

It is obvious that drivers are missing but I have no idea how to install them. I already used

sudo pacman -S php
sudo pacman -S php-sqlite
sudo pacman -S mysql

Maybe I forgot some. Here is my /etc/php/php.ini file’s content

;;;;;;;;;;;;;;;;;;;;;;
; Dynamic Extensions ;
;;;;;;;;;;;;;;;;;;;;;;

; If you wish to have an extension loaded automatically, use the following
; syntax:
;
;   extension=modulename
;
; For example:
;
;   extension=mysqli
;
; When the extension library to load is not located in the default extension
; directory, You may specify an absolute path to the library file:
;
   extension=/path/to/extension/mysqli.so
;
; Note : The syntax used in previous PHP versions ('extension=<ext>.so' and
; 'extension='php_<ext>.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.
;
;extension=bcmath
;extension=bz2
;extension=calendar
extension=curl
;extension=dba
;extension=enchant
;extension=exif
;extension=ftp
extension=gd
;extension=gettext
;extension=gmp
;extension=iconv
;extension=imap
;extension=intl
;extension=sodium
;extension=ldap
;extension=mysqli
;extension=odbc
;zend_extension=opcache
;extension=pdo_dblib
extension=pdo_mysql
extension=bz2.so
extension=mcrypt.so
extension=mysqli
;extension=pdo_odbc
;extension=pdo_pgsql
extension=pdo_sqlite
;extension=pgsql
;extension=pspell
;extension=shmop
;extension=snmp
;extension=soap
;extension=sockets
extension=sqlite3
;extension=sysvmsg
;extension=sysvsem
;extension=sysvshm
;extension=tidy
;extension=xmlrpc
;extension=xsl
extension=zip

It didn’t let me upload the whole file so here is only part of it. If you need more details I will gladly add them in comments.
Maybe I am missing something obvious but I can’t find it for a few days.

EDIT
Sorry for not pointing this out.
I am using Manjaro Linux OS

3

Answers


  1. I think PDO extensions are missing. Add the below extensions and restart your server

    For windows server –

    extension=php_pdo.dll
    extension=php_pdo_mysql.dll
    

    For Linux server –

    extension=pdo.so 
    extension=pdo_mysql.so
    
    Login or Signup to reply.
  2. Try this

    sudo apt-get install php7-mysql
    
    Login or Signup to reply.
  3. When creating the connection, your DSN has localhost as the database type…

    $dbh = new PDO('localhost:host=$servername;dbname=test', $username, $password);
    

    from the manual

    $dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
    

    Also as you use single quotes, the servername will not be replaced, so use

    $dbh = new PDO("mysql:host=$servername;dbname=test", $username, 
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search