Php – Fatal error: Uncaught exception ‘PDOException’ with message ‘could not find driver’

databasemysqlpdophpxampp

I'm new to web development and today, just as I was practicing with a local server (using XAMPP Apache and MySQL services), I got this error message:

 Fatal error: Uncaught exception 'PDOException' with message 'could not find driver' in C:\xampp\htdocs\blog\resources\query functions.php:3 Stack trace: #0 C:\xampp\htdocs\blog\resources\query functions.php(3): PDO->__construct('dblib:host=loca...', 'admin', 'root') #1 C:\xampp\htdocs\blog\resources\main.php(23): include('C:\xampp\htdocs...') #2 {main} thrown in C:\xampp\htdocs\blog\resources\query functions.php on line 3

There's index.php file in C:\xampp\htdocs\blog that includes main.php from C:\xampp\htdocs\blog\resources, then main.php includes another file in same directory called queryfunctions.php. main.php only has one line of code and that is to include queryfunctions.php. Then in that file I wrote this:

$database = new PDO('dblib:host=localhost;dbname=test;charset=UTF-8', "admin", "root");

And then it gives me the error whenever I go to test it. I've read through a couple of questions and answers already, but none helped. In my PHP info, these are the PDO drivers installed: mysql, odbc, sqlite, sqlite2.

PDO Driver for MySQL: Client API Version mysqlnd 5.0.8-dev – 20102224
– $Revision: 310735 $.

Best Solution

If you are trying to connect to MySQL, use the MySQL connection string:

$database = new PDO('mysql:host=localhost;dbname=test', "admin", "root");

The dblib driver you are using is for Microsoft SQL and Sybase. If you are using XAMPP, chances are you are using MySQL not Microsoft SQL. If you are using Microsoft SQL, you will need to download and install it: http://www.php.net/manual/en/ref.pdo-dblib.php

Related Question