How to connect PHP 5.6 to Sql Server 2008?

I need to connect to a database MS Sql Server 2008 with PHP 5.6

I have Development: Windows 7 64bits, with XAMPP

Production Centos 7

I installed the Microsoft drivers .

With this line of code

$c = new PDO("sqlsrv:Server=$host;Database=$db", "$user", "$pwd");

Gives error:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[IMSSP]: this extension requires the Microsoft ODBC Driver 11 for SQL Server to communicate with SQL Server. Access the following URL to download the ODBC Driver 11 for SQL Server for x86

Probably won't work on Centos 7.

What is the best solution?

Author: Marco Souza, 2015-09-23

3 answers

Some files will be required .dll that you will be able to find in the following link: https://www.microsoft.com/en-us/download/details.aspx?id=20098 Since your PHP is 5.6, download the SQLSRV32 file.

After downloading the file, you will extract it, there you will have the necessary dlls.

Use phpinfo (), to check which PHP Extension Build. PHP Extension Build. in the case of the example you will have to copy the dlls that have _ts.php 5.6 dll, i.e.: php_pdo_sqlsrv_56_ts.dll e php_sqlsrv_56_ts.dll, Copy these files to their respective directories, C:/.../php/ext and also for C:/windows/system32/ and/or for C:/windows/SysWOW64 if it is 64bit computer.

Once this is done you must open php.ini and in the extension session you should add the following lines:

Extension=php_sqlsrv_56_ts.dll

Extension = php_pdo_sqlsrv_56_ts.dll

php.iniYou will also need to install Microsoft ® ODBC Driver for SQL Server [Native Client] that will depend on your bank in the case as an example I will pass the Microsoft® ODBC Driver 11 link to SQL Server.

Https://www.microsoft.com/pt-br/download/details.aspx?id=36434

After complete installation of the previous steps. you must restart apache.

If everything is correct you can access phpinfo () again and you can search for sqlsrv

And then by pdo_sqlsrv pdo_sqlsrv
(source: synet.sk)
I hope to have help.

 2
Author: Marcus Italo, 2019-03-16 17:51:57

Man, would you like to try the connection using the ODBC Driver? Do not seise help in your case, but with me it worked out like this:

$conexao = new PDO("odbc:Driver={SQL Server};Server=.\SQLEXPRESS;Database=bancoDeDados; Uid=sa;Pwd=SenhaDoBanco;");

Remembering that vc must enable the ODBC driver in PHP.ini...

 0
Author: Printf, 2016-09-15 15:27:05

I can connect in Sql Server 2008 with:

  • Debian 8 and PHP 5.6 I use PDO with drive dblib
  • Ubuntu 16.04 and PHP 7.0 I use PDO with drive sqlsrv, if you wish here is DockerFile link to mount this machine
/***
 *  Get Data Source Name (DSN) do PDO
 *  Deixando o Semelhante getDsnPDO do formin, para evitar o problema da versão do Drive
 *  https://intratec.mpdft.mp.br/gitlab/Repositorio/Infraestrutura/Utils/formDin/blob/master/classes/webform/TPDOConnection.class.php#L481
 * @param string $host      - IP ou nome do servidor
 * @param string $database  - nome do banco
 * @return NULL|string
 */
function getDsnPDO($sgbd,$host,$database) {
    $dsn = null;

    if($sgbd=="mysql"){
        $port='3306';
        $dsn = 'mysql:host='.$host.';dbname='.$database.';port='.$port;
    } else {
        $port = '1433';
        if (PHP_OS == "Linux") {
            if (version_compare(PHP_VERSION, '7.0.0') >= 0) {
                $driver = 'sqlsrv';
                $dsn = $driver.':Server='.$host.','.$port.';Database='.$database;
            } else {
                $driver = 'dblib';
                //$dsn = $driver.':version=7.2;charset=UTF-8;host=' . HOST . ';dbname=' . DATABASE . ';port=' . PORT;
                $dsn = $driver.':version=7.2;host='.$host.';dbname='.$database.';port='.$port;
            }
        } else {
            $driver = 'sqlsrv';
            $dsn = $driver.':Server='.$host.','.$port.';Database='.$database;
        }
    }
    return $dsn;
}

function conecta() {        


    $hostname       = 'meuservidorbanco';
    $dbname         = 'banco';
    $username       = 'usuario';
    $password       = 'senha';


    try{
        $stringPDO = getDsnPDO($sgbd,$hostname,$dbname);
        $dbh = new PDO($stringPDO,$username,$password);
        $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    }
    catch(PDOException $e){
        echo $e->getMessage();
        exit;
    }
    return $dbh;
}
 0
Author: Bjverde, 2019-07-10 16:29:02