PHP SQL Server Couldn't connect

17,918

Solution 1

You just need to install driver from this link PHP Driver

You can follow this link for tutorial PHP MSSQL Tutorial

An other option is to connect to mssql using DSN ,
and this easy way you just to enable mssql extension in php

And follow this link to create DSN Create DSN in windows

Also follow this link to connect using php and DSN Connect php using DSN

Solution 2

To connect with sql server you need to add dll file for sql server in the directory

wamp\bin\php\php5.3.0\ext

Solution 3

Try PDO:

//$pdo = new PDO("sqlsrv:Server=$hostname;Database=$dbname;", $username, $password);  // works with proper driver for PHP.
$pdo = new PDO("odbc:Driver={SQL Server};Server=$hostname;Database=$dbname;", $username, $password);  // works with proper driver for ODBC and PHP ODBC.

I could not get the first line to work due to weird compiler version incompatibilities, but the second one worked fine after installing Microsoft ODBC Driver 11 for SQL Server

PHP Version 5.3.0 has built-in ODBC support, according to php.ini, but that still lists an active extension=php_pdo_odbc.dll here.

Share:
17,918
Kongkow
Author by

Kongkow

Updated on June 14, 2022

Comments

  • Kongkow
    Kongkow almost 2 years

    I want to ask you about how to connect to MSSQL Server 2005 using PHP?

    I got error like this :

    Connection could not be established. Array ( [0] => Array ( [0] => IMSSP [SQLSTATE] => IMSSP [1] => -49 [code] => -49 [2] => This extension requires the Microsoft SQL Server 2012 Native Client. Access the following URL to download the Microsoft SQL Server 2012 Native Client ODBC driver for x86: http://go.microsoft.com/fwlink/?LinkId=163712 [message] => This extension requires the Microsoft SQL Server 2012 Native Client. Access the following URL to download the Microsoft SQL Server 2012 Native Client ODBC driver for x86: http://go.microsoft.com/fwlink/?LinkId=163712 ) [1] => Array ( [0] => IM002 [SQLSTATE] => IM002 [1] => 0 [code] => 0 [2] => [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified [message] => [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified ) )

    and here is my PHP code :

    <?php
        $serverName = "192.168.183.249\MSSQLSERVER, 1542"; //serverName\instanceName, portNumber default is 1433)
        $connectionInfo = array( "Database"=>"SEI_AproCS", "UID"=>"sa", "PWD"=>"");
        $conn = sqlsrv_connect( $serverName, $connectionInfo);
        if( $conn ) {
         echo "Connection established.<br />";
        }else{
         echo "Connection could not be established.<br />";
         die( print_r( sqlsrv_errors(), true));
        }
    ?>
    

    Please help to advice.

    THanks

  • Kongkow
    Kongkow over 10 years
    I just followed that step and still can't connect :(
  • Samy Massoud
    Samy Massoud over 10 years
    is mssql local at your machine ? and do you have to use this method ?
  • Kongkow
    Kongkow over 10 years
    I using php_sqlsrv_54_ts.dll, not mssql. I have tried using mssql but for PHP 5.4 cant
  • Samy Massoud
    Samy Massoud over 10 years
    @Kongkow see the new edits , and don't forget to install mssql server at your local machine if you use local mssql database .