Unable to connect to msSQL database via PHP

前端 未结 9 382
余生分开走
余生分开走 2021-01-13 07:30

I am using the current code in attempt to access a msSQL 2005 db:



        
相关标签:
9条回答
  • 2021-01-13 08:17

    Late response, but it might help someone. we are nearly there, it is the problem with your connection parameters, probably you need to give servername as IP:port

    servername:: The MS SQL server. e.g. hostname:port (Linux), or hostname,port (Windows).

    Server Name should be "server\instance" An instance is nothing more than specific port address different than 1433... so just discover the port on mssql server and then try to connect using: ip:port

    Example code that works completely fine for me::

        ini_set('display_errors', '1');
        // $myServer = "winsrv22.somedns.co.uk:22320";//this style works as well
        $servername = "123.21.47.23:22320";
        $myUser = "UserName";
        $myPass = 'xxxxxxxx';
        $myDB = "[database]"; 
    
        //connection to the database
        $dbhandle = mssql_connect($servername, $myUser, $myPass)
            or die("Couldn'tt connect to SQL Server on $myServer"); 
        if($dbhandle) {
         echo "Success, Connected\r\n";
        } else {
            echo "problem :( \r\n";
        }
    

    Hope this helps some one :) Happy coding!!

    0 讨论(0)
  • 2021-01-13 08:30

    I had some difficulties with this a few months ago, and I found that the only way to make it work was to include the instance name when specifying the server. For example:

    $myServer = "SERVER\INSTANCENAME";
    

    Specifying just the server would not work, even with TCP/IP enabled.

    0 讨论(0)
  • 2021-01-13 08:35

    stop using

    mssql_connect

    and start using

    sqlsrv_connect

    That will save you a lot of headaches. Plus, the function *mssql_connect* has been deprecated.

    For using sqlsrv_connect, you must download the driver and install it as an extension for PHP to recognize the sqlsrv functions. Download the driver from Microsoft Download Center (search for "sql server php driver"), at the time of this post the download url was: http://www.microsoft.com/en-us/download/details.aspx?id=20098

    The right steps for installation are clearly explained by Microsoft itself at http://www.microsoft.com/en-us/download/details.aspx?id=20098

    1. download the driver. 2. put it in the PHP ext folder. 3. update the php.ini 4. restart the server.

    After installing the sql server driver, then simply follow the instructions from http://www.php.net/manual/en/function.sqlsrv-connect.php. Below a quick snippet:

    <?php
    $serverName = "serverName\sqlexpress"; //serverName\instanceName
    
    // Since UID and PWD are not specified in the $connectionInfo array,
    // The connection will be attempted using Windows Authentication.
    $connectionInfo = array( "Database"=>"dbName");
    $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));
    }
    ?>
    

    Vote up!

    0 讨论(0)
提交回复
热议问题