简体   繁体   中英

PHP SQLSRV connection “Failed to get DB handle: could not find driver”

I'm using the following code:

try {
    $hostname = "*****";
    $port = 1443;
    $dbname = "*******";
    $username = "********";
    $pw = "************";
    $dbh = new PDO ("dblib:host=$hostname:$port;dbname=$dbname","$username","$pw");
} catch (PDOException $e) {
    echo "Failed to get DB handle: " . $e->getMessage() . "\n";
    exit;
}

Error:

Failed to get DB handle: could not find driver

The DLL Files are already in the php/ext and modified in the php.ini

What else can I do?

From the port you're using, it's clear that you're trying to connect to an MSSQL database.

PHP documentation states that the PDO support for connecting to SQLSRV is not recommended for windows machines running php 5.3 or older.

http://php.net/manual/en/ref.pdo-dblib.php

They do suggest using sqlsrv drivers provided by microsoft.

Link to sqlsrv drivers are in the php link above.

Have you tried mssql_connect?

Here's the syntax: mssql_connect ([ string $servername [, string $username [, string $password [, bool $new_link = false ]]]] )

The implementation would be:

$link = mssql_connect($hostname, $username, $pw);
mssql_select_db($dbname, $link);

Note that $hostname should contain MSSQL Instance Name. eg 'KALLESPC\\SQLEXPRESS'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM