LMORSE
LMORSE

Reputation: 41

LINUX PDO_SQLSRV Could not find driver unable to connect

Hoping you can help. I am running Linux Ubunto 22.04 with PHP8.1.

I installed the sqlsrv drivers following this page: Loading the Microsoft Drivers for PHP for SQL Server

I am still getting a 'could not find driver' error message on PDO("sqlsrv:server=

I had to install the drivers by downloading the pre-built drivers (package Ubuntu2204-8.1). I copied the driver files to /usr/lib/php/20210902', based on: php -i | grep extension_dir

The file '10-pdo.ini' was pre-existing in /etc/php/8.1/apache2/conf.d/.

I created a new file '30-pdo-sqlsrv' as instructed linking to 'extension=php_pdo_sqlsrv_82_nts.so'.

I added the following to the bottom of my php.ini file: extension=php_sqlsrv_81_nts.so (I checked that I edited the correct php.ini file by checking the path in phpinfo:/etc/php/8.1/apache2/php.ini)

I have restarted my server ('sudo service apache2 restart'), and I list my PHP modules ('php -m'). The list shows, amonsgt others: PDO and pdo_sqlsrv.

PHPINFO lists the following: PDO PDO support enabled PDO drivers mysql pdo_mysql PDO Driver for MySQL enabled Client API version mysqlnd 8.1.2-1ubuntu2.14 Directive Local Value Master Value pdo_mysql.default_socket /var/run/mysqld/mysqld.sock /var/run/mysqld/mysqld.sock

I can see nothing about pdo_sqlsrv

I created some code to test the connection:

    if (!function_exists('mysqli_init') && !extension_loaded('mysqli')) 
{
    echo '!!We don\'t have mysqli!!!'. "<br>";
} 
else 
{
    echo 'msyqli function exists.'. "<br>";
}
$mysqli = null;

if (!extension_loaded('pdo')) 
{
    echo 'We don\'t have PDO!!!'. "<br>";
} 
else 
{
    echo 'PDO extension loaded.'. "<br>";
}



// pdo_sqlsrv pdo_sqlsrv
if (!function_exists('pdo_sqlsrv')) 
{
    echo '!!pdo_sqlsrv does not exist!!!'. "<br>";
} 
else 
{
    echo 'pdo_sqlsrv extension exists.'. "<br>";
}
if (!extension_loaded('pdo_sqlsrv')) 
{
    echo '!!pdo_sqlsrv not loaded!!!'. "<br>";
} 
else 
{
    echo 'pdo_sqlsrv extension loaded.'. "<br>";
}

try
{
$connection = new PDO("sqlsrv:server=$servername;Database=$database", $user, $password);


} 
catch (PDOException $e) 
{
    echo 'PDO Connection failed: ' . $e->getMessage(). "<br>";
}

My code outputs the following:

Can anyone point me to something that I might have missed? Why is this not working? Am I barking up the wrong tree with what I have tried?

Upvotes: 0

Views: 473

Answers (1)

LMORSE
LMORSE

Reputation: 41

I ended up re-installing drivers for sqlsrv.

Upvotes: 0

Related Questions