SQL Server address to connect using php

后端 未结 1 600
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-17 07:02

I use this code to connect to SQL Server 2012, but it does not work



        
相关标签:
1条回答
  • 2021-01-17 07:48

    If your edition is SQL Server Express, you should probably be using:

    $objConnect = mssql_connect("localhost\SQLEXPRESS","usr","pass");
    

    Or if it is otherwise a named instance, then

    $objConnect = mssql_connect("localhost\InstanceName","usr","pass");
    

    If you need to connect remotely, then obviously you shouldn't be using localhost since how does the remote web server locate your localhost? You should be using one of the following (assuming the remote web server can see your machine with IP address 192.168.5.22):

    $objConnect = mssql_connect("192.168.5.22\SQLEXPRESS","usr","pass");
    $objConnect = mssql_connect("192.168.5.22\NamedInstance","usr","pass");
    $objConnect = mssql_connect("192.168.5.22","usr","pass");
    

    Of course your firewall must have port 1433 (and possibly 1434) open in order to accept that connection, and there are a variety of other things that can go wrong here as well.

    However, a little debugging 101 suggestion. Instead of:

    if($objConnect)  
    {  
      echo "Database Connected.<br />";  
      echo mssql_error();
    }  
    else  
    {  
      echo "Database Connect Failed.<br />";  
    }  
    

    Why not:

    if($objConnect)  
    {
      echo "Database Connected.<br />";  
    }  
    else  
    {  
      echo "Database Connect Failed.<br />";  
      echo mssql_error();
    }  
    

    Surely you don't need to write an error to the page when the database connects successfully. And telling us the actual error message you receive may better equip us to point you in the direction of a solution. A generic "Database Connect Failed" message that you wrote is not going to give anyone any clue about what actually went wrong. But I bet mssql_error() might!

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