skip to Main Content

I am trying to connect database in sql server 2014 with php as follows:

First I have this script in conexaosql.php:

class Conexao
{
    private static $connection;

    private function __construct(){}

    public static function getConnection() {

        $pdoConfig  = DB_DRIVER . ":". "Server=" . DB_HOST . ";";
        $pdoConfig .= "Database=".DB_NAME.";";

        try {
            if(!isset($connection)){
                $connection =  new PDO($pdoConfig, DB_USER, DB_PASSWORD);
                $connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            }
            return $connection;
         } catch (PDOException $e) {
            $mensagem = "Drivers disponiveis: " . implode(",", PDO::getAvailableDrivers());
            $mensagem .= "nErro: " . $e->getMessage();
            throw new Exception($mensagem);
         }
     }
}

Then I call this script and I have the following code:

define('DB_HOST'        , "xxxx");
define('DB_USER'        , "xxxx");
define('DB_PASSWORD'    , "xxxx");
define('DB_NAME'        , "xxxx");
define('DB_DRIVER'      , "sqlsrv");

require ("conexaosql.php");

try{

    $Conexao    = Conexao::getConnection();
    $query      = $Conexao->query("SELECT Pago FROM UTE02.dbo.Recibos");
    $produtos   = $query->fetchAll();

 }catch(Exception $e){

    echo $e->getMessage();
    exit;

 }

I get the following error when I run the code:

mysql,sqlite Erro: could not find driver

I’m using php 8.1 and apache. I leave the link with images from phpinfo()

2

Answers


  1. Chosen as BEST ANSWER

    I solved my problem by running the following commands:

    # Microsoft ODBC 17
    sudo su
    curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
    
    #Download appropriate package for the OS version - Ubuntu 18.04
    curl https://packages.microsoft.com/config/ubuntu/18.04/prod.list > /etc/apt/sources.list.d/mssql-release.list
    exit
    sudo apt-get update
    sudo ACCEPT_EULA=Y apt-get install msodbcsql17
    
    # optional: for bcp and sqlcmd
    sudo ACCEPT_EULA=Y apt-get install mssql-tools
    echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bash_profile
    echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc
    source ~/.bashrc
    # optional: for unixODBC development headers
    sudo apt-get install unixodbc-dev
    
    # Microsoft ODBC 17
    # 8.1
    sudo apt-get -y install php-pear php8.1-dev
    sudo update-alternatives --set php /usr/bin/php8.1
    sudo update-alternatives --set phar /usr/bin/phar8.1
    sudo update-alternatives --set phar.phar /usr/bin/phar.phar8.1
    sudo update-alternatives --set phpize /usr/bin/phpize8.1
    sudo update-alternatives --set php-config /usr/bin/php-config8.1
    
    sudo pecl uninstall -r sqlsrv 
    sudo pecl uninstall -r pdo_sqlsrv 
    sudo pecl -d php_suffix=8.1 install sqlsrv
    sudo pecl -d php_suffix=8.1 install pdo_sqlsrv
    sudo su
    printf "; priority=20nextension=sqlsrv.son" > /etc/php/8.1/mods-available/sqlsrv.ini
    printf "; priority=30nextension=pdo_sqlsrv.son" > /etc/php/8.1/mods-available/pdo_sqlsrv.ini
    exit
    sudo phpenmod -v 8.1 sqlsrv pdo_sqlsrv
    sudo service apache2 restart
    

  2. You need to install the PDO_SQLSRV driver to access the database. You have only installed support for MySQL and SQLite.

    If you run a Linux server, you have to install and use ODBC.

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