Jump to content

MSSQL PDO could not find driver


ilkist

Recommended Posts

I'm using PHP Version 5.3.8 that was installed via XAMPP along with Microsoft SQL Server 2008 R2 (SQLEXPRESS). I have the drivers installed correctly (I think) and have added the correct line into php.ini (extension=php_pdo_sqlsrv_53_ts_vc9.dll to be exact). I'm trying to connect to the server like so:

try {
$DBH = new PDO("mssql:host=xxxx;dbname=xxxx", 'xxxx', 'xxxx');
}
catch(PDOException $e) {
echo $e->getMessage();
}

 

I get the "could not find driver" error, and I've tweaked it all kinds of ways to solve the problem. I've tried all other kinds of drivers, but this is the only one that Apache doesn't give me an error on startup. When I run phpinfo(), the pdo_sqlsrv fields are all blank except pdo_sqlsrv.log_severity which is set to 0.

 

I DL'd my drivers from http://www.microsoft.com/en-us/download/details.aspx?id=20098, and I've tried both 2.0 and 3.0

 

Any advice would be awesome!!

Link to post
Share on other sites

**EDIT**

 

I just solved my own problem...

 

instead of

try {
$DBH = new PDO("mssql:host=xxxx;dbname=xxxx", 'xxxx', 'xxxx');
}
catch(PDOException $e) {
echo $e->getMessage();
}

 

I did

try {
$DBH = new PDO("sqlsrv:host=xxxx;dbname=xxxx", 'xxxx', 'xxxx');
}
catch(PDOException $e) {
echo $e->getMessage();
}

 

I was using the old method of connecting to the DB...

Link to post
Share on other sites

I don't want to confuse anyone, so here's a 100% working example of connecting to a MS SQL server using PDO. This method has detailed error reporting enabled.

 

$servername = "xxxxx";
$database = "xxxxx";
$user = "xxxxx";
$pass = "xxxxx";

try {
$DBH = new PDO('sqlsrv:Server='.$servername.';Database='.$database, $user, $pass);
$DBH->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
}
catch(PDOException $e) {
echo $e->getMessage();
}

Link to post
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.