skip to Main Content

I have a Laravel installation running on a Debian 9.9 server and PHP 7.4.

I need to connect to a SQL Server database on a remote Windows server.

I followed this guide to install the PHP extensions:
https://learn.microsoft.com/en-us/sql/connect/php/installation-tutorial-linux-mac?view=sql-server-ver15#installing-the-drivers-on-debian-8-9-and-10

And this one to install the ODBC drivers:
https://learn.microsoft.com/en-us/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server?view=sql-server-ver15

I also tried reinstalling PHP.

But even after doing all this, I still can’t connect to the database and get this error:

could not find driver (SQL: select * from [table] where [column] = 123)

Thanks

EDIT

This is are the lines where PDO is mentioned in the result of phpinfo()

PDO support => enabled
PDO drivers => mysql, sqlsrv

pdo_sqlsrv

pdo_sqlsrv support => enabled
ExtensionVer => 5.8.0

Directive => Local Value => Master Value
sqlsrv.ClientBufferMaxKBSize => 10240 => 10240
sqlsrv.LogSeverity => 0 => 0
sqlsrv.LogSubsystems => 0 => 0
sqlsrv.SetLocaleInfo => 2 => 2
sqlsrv.WarningsReturnAsErrors => On => On

php –ini shows that these two extensions are enabled:
/etc/php/7.4/cli/conf.d/pdo_sqlsrv.ini,
/etc/php/7.4/cli/conf.d/sqlsrv.ini

PDO::getAvailableDrivers() returns the following:

Array
(
    [0] => mysql
    [1] => sqlsrv
)

3

Answers


  1. Chosen as BEST ANSWER

    Turns out the problem was the Apache configuration. Even after reinstalling it, for some reason I had still an old configuration loaded. I had installed the driver on 7.4 and apache was still serving 7.3.

    I solved it by doing:

    sudo a2dismod php7.3
    sudo a2enmod php7.4
    sudo service apache2 restart
    

  2. Please try the following, create a php file (ex: PDO.php) in some directory and insert the following content into it:

    <?php
        print_r(PDO::getAvailableDrivers());
    ?>
    

    then run this file as follows:

    leo@alpha:~$ php PDO.php
    

    Terminal should return an array of available PDO drivers. This way you can check if PHP really has the SQL driver enabled. In my case:

    Array
    (
        [0] => mysql
        [1] => pgsql
        [2] => sqlsrv
    )
    

    Also check that in the Laravel directory, the .env file has correctly configured the connection parameters to the SQL server …

    DB_CONNECTION=sqlsrv
    DB_HOST=localhost
    DB_PORT=1433
    DB_DATABASE=project
    DB_USERNAME=sa
    DB_PASSWORD=password
    

    I wish you can solve your problem.

    * EDIT 1 *

    Please try the following:

    First, create a file inside the root directory of your web server (ex: index.php), and inside it, insert the following content:

    <?php
        php_info();
    ?>
    

    access that file through your browser, and it should return all the php settings that apache is using. Look in the PDO section, if you have the SQL Server driver loaded ..

    Login or Signup to reply.
  3. Debian 9/10

    Install Sury APT repository

    sudo apt update
    sudo apt install -y lsb-release ca-certificates apt-transport-https software-properties-common
    echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/sury-php.list
    wget -qO - https://packages.sury.org/php/apt.gpg | sudo apt-key add -
    sudo apt update
    

    Install your desired php version

    sudo apt install php7.2  php7.4  php8.0
    

    Install additional desired modules (change x.x with version)

    sudo apt install phpx.x-{mysql,cli,common,imap,ldap,xml,fpm,curl,mbstring,zip}
    

    For php 7.2:

    sudo update-alternatives --set php /usr/bin/php7.2
    sudo update-alternatives --set phar /usr/bin/phar7.2
    sudo update-alternatives --set phar.phar /usr/bin/phar.phar7.2
    sudo update-alternatives --set phpize /usr/bin/phpize7.2
    sudo update-alternatives --set php-config /usr/bin/php-config7.2
    
    sudo apt install php7.2-dev
    
    sudo pecl config-set php_ini /etc/php/7.2/fpm/php.ini
    sudo pecl config-set ext_dir /usr/lib/php/20170718/
    
    sudo pecl -d php_sufix=7.2 install sqlsrv
    sudo pecl -d php_sufix=7.2 install pdo_sqlsrv
    sudo su
    printf "; priority=20nextension=sqlsrv.son" > /etc/php/7.2/mods-available/sqlsrv.ini
    printf "; priority=30nextension=pdo_sqlsrv.son" > /etc/php/7.2/mods-available/pdo_sqlsrv.ini
    exit
    sudo phpenmod -s fpm -v 7.2 sqlsrv pdo_sqlsrv
    sudo phpenmod -s apache2 -v 7.2 sqlsrv pdo_sqlsrv
    sudo phpenmod -s cli -v 7.2 sqlsrv pdo_sqlsrv
    
    sudo service php7.2-fpm restart
    sudo service apache2 restart
    

    For php 7.4:

    sudo update-alternatives --set php /usr/bin/php7.4
    sudo update-alternatives --set phar /usr/bin/phar7.4
    sudo update-alternatives --set phar.phar /usr/bin/phar.phar7.4
    sudo update-alternatives --set phpize /usr/bin/phpize7.4
    sudo update-alternatives --set php-config /usr/bin/php-config7.4
    
    sudo apt install php7.4-dev
    
    sudo pecl config-set php_ini /etc/php/7.4/fpm/php.ini
    sudo pecl config-set ext_dir /usr/lib/php/20190902
    
    sudo pecl -d php_sufix=7.4 install sqlsrv
    sudo pecl -d php_sufix=7.4 install pdo_sqlsrv
    sudo su
    printf "; priority=20nextension=sqlsrv.son" > /etc/php/7.4/mods-available/sqlsrv.ini
    printf "; priority=30nextension=pdo_sqlsrv.son" > /etc/php/7.4/mods-available/pdo_sqlsrv.ini
    exit
    sudo phpenmod -s fpm -v 7.4 sqlsrv pdo_sqlsrv
    sudo phpenmod -s apache2 -v 7.4 sqlsrv pdo_sqlsrv
    sudo phpenmod -s cli -v 7.4 sqlsrv pdo_sqlsrv
    
    sudo service php7.4-fpm restart
    sudo service apache2 restart
    

    For php 8.0:

    sudo update-alternatives --set php /usr/bin/php8.0
    sudo update-alternatives --set phar /usr/bin/phar8.0
    sudo update-alternatives --set phar.phar /usr/bin/phar.phar8.0
    sudo update-alternatives --set phpize /usr/bin/phpize8.0
    sudo update-alternatives --set php-config /usr/bin/php-config8.0
    
    sudo apt install php8.0-dev
    
    sudo pecl config-set php_ini /etc/php/8.0/fpm/php.ini
    sudo pecl config-set ext_dir /usr/lib/php/20200930
    
    sudo pecl -d php_sufix=8.0 install sqlsrv
    sudo pecl -d php_sufix=8.0 install pdo_sqlsrv
    sudo su
    printf "; priority=20nextension=sqlsrv.son" > /etc/php/8.0/mods-available/sqlsrv.ini
    printf "; priority=30nextension=pdo_sqlsrv.son" > /etc/php/8.0/mods-available/pdo_sqlsrv.ini
    exit
    sudo phpenmod -s fpm -v 8.0 sqlsrv pdo_sqlsrv
    sudo phpenmod -s apache2 -v 8.0 sqlsrv pdo_sqlsrv
    sudo phpenmod -s cli -v 8.0 sqlsrv pdo_sqlsrv
    
    sudo service php8.0-fpm restart
    sudo service apache2 restart
    

    Check loaded modules

    php -m
    

    Or just check sqlsrv

    php -m | grep sqlsrv
    

    Common trouble:

    pecl extension fails because already installed

    Just uninstall it:

    sudo pecl -d php_sufix=7.2 uninstall sqlsrv
    sudo pecl -d php_sufix=7.2 uninstall pdo_sqlsrv
    
    sudo pecl -d php_sufix=7.4 uninstall sqlsrv
    sudo pecl -d php_sufix=7.4 uninstall pdo_sqlsrv
    
    sudo pecl -d php_sufix=8.0 uninstall sqlsrv
    sudo pecl -d php_sufix=8.0 uninstall pdo_sqlsrv
    

    then try again.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search