I'm using the following code to try and connect using:
try {
$hostname = "***********";
$port = *****;
$dbname = "******";
$username = "******";
$pw = "********";
$dbh = new PDO ("sqlsrv:host=$hostname:$port;dbname=$dbname","$username","$pw");
} catch (PDOException $e) {
echo "Failed to get DB handle: " . $e->getMessage() . "
";
exit;
}
but I'm getting the following error:
Failed to get DB handle: could not find driver
How do I fix this error?
I've seen other answers say that I should have "SQL Server PDO Driver" installed. Is there a way to check this is installed?
If your operating system you are running php is Windows, then you have to setup SQL Server php extension as described here https://technet.microsoft.com/en-us/library/cc793139(v=sql.90).aspx. If you run PHP on Linux then you have to install the extension php-mssql or odbc drivers:
PHP and Accessing MS SQL Server in Unix/Linux: http://www.codeproject.com/Tips/607814/PHP-and-Accessing-MS-SQL-Server-in-Unix-Linux
Connect remotely to MSSQL with PHP: https://www.centos.org/forums/viewtopic.php?t=21567
Also check this related question and answer: Connecting to mssql using pdo through php and linux