How to connect PHP 5.6 to Sql Server 2008?

Asked

Viewed 2,517 times

0

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

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");

Makes a mistake:

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 in Centos 7.

What is the best solution?

3 answers

2

You will need some . dll files that you can find in the following link: https://www.microsoft.com/en-us/download/details.aspx?id=20098 Since your PHP is 5.6, Download 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.dll from php 5.6, i.e.: php_pdo_sqlsrv_56_ts.dll and php_sqlsrv_56_ts.dll, Copy these files to their directories, C:/.../php/ext and also for the C:/windows/system32/ and/or for the C:/windows/SysWOW64 if you are already a 64bit computer.

Once this is done you should 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.ini It will also be necessary to install Microsoft® ODBC Driver for SQL Server[Native Client] which will depend on your database in case as an example I will pass the link of Microsoft® ODBC Driver 11 to SQL Server.

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

After installing the previous steps. you should restart apache.

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

and then by pdo_sqlsrv pdo_sqlsrv
(source: synet.sk)
hope I’ve helped.

0

I can connect to Sql Server 2008 with:

  • Debian 8 and PHP 5.6 use PDO with dblib drive
  • Ubuntu 16.04 and PHP 7.0 using PDO with sqlsrv drive, if desired here is link to Dockerfile to assemble that 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

Dude, could you try the connection using the ODBC Driver? It doesn’t help your case, but it worked for me like this:

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

Remember that you must enable the ODBC driver in PHP.ini...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.