PHP connection sqlserver2008, how to connect the database on the specified IP address

Source: Internet
Author: User
Tags sql server books
$serverName = "(local)"; Database server address $uid = "sa"; Database user Name $pwd = "Password123"; Database Password $connectioninfo = array ("UID" = = $uid, "PWD" = = $pwd, "database" = "test"); $conn = Sqlsrv_connect ($ ServerName, $connectionInfo); if ($conn = = False) {    echo ' connection failed! ";    Die (Print_r (Sqlsrv_errors (), True));} if ($conn) {echo "Connection succeeded";}

The connection was successful, but how to change the local to 127.0.0.1.


Reply to discussion (solution)

Switch directly to an IP address

$serverName = "IP Address";
Or
$serverName = "Computer name";
If there are multiple instances on the server, you also need to have the instance name

Remote connection also requires remote connection service
SQL attacks are prevalent due to the default remote connection of SQL Server 2000.
So now all learned, without the cumbersome remote connection authorization, is unable to connect

Header ("content-type:text/html; charset=gb2312 "); $serverName =" 127.0.0.1/mssqlserver "; Database server address $uid = "sa"; Database user Name $pwd = "Password123"; Database Password $connectioninfo = array ("UID" = = $uid, "PWD" = = $pwd, "database" = "test"); $conn = Sqlsrv_connect ($ ServerName, $connectionInfo);//$conn = new PDO ("Sqlsrv:server=127.0.0.1;database=test", "sa", "yyyy1111") or Die ("PDO Connection faild. "); if ($conn = = False) {    echo ' connection failed! ";    Die (Print_r (Sqlsrv_errors (), True));} if ($conn) {echo "Connection succeeded";}

Results:
Connection Failed! Array ([0] = = Array ([0] = 08001 [SQLSTATE] = 08001 [1] = [code] = [2] = [Microsoft][sql Se RVer Native Client 11.0]named Pipes provider:could not open a connection to SQL Server [67]. [Message] = [Microsoft][sql Server Native Client 11.0]named Pipes provider:could not open a connection to SQL server [1] = = Array ([0] = HYT00 [SQLSTATE] = HYT00 [1] = 0 [Code] = 0 [2] = [Microsoft][sql Serv Er Native client 11.0]login timeout expired [message] = [Microsoft][sql Server Native Client 11.0]login timeout expire D) [2] = = Array ([0] = 08001 [SQLSTATE] = 08001 [1] = [code] = [2] = [Microsoft][sql Server Native Client 11.0]a network-related or instance-specific error have occurred while establishing A connection to SQL Serve R. Server is not a found or not accessible. Check If instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL SeRVer Books Online. [Message] = [Microsoft][sql Server Native Client 11.0]a network-related or instance-specific error have occurred while Establishing a connection to SQL Server. Server is not a found or not accessible. Check If instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online. ) )

Header ("content-type:text/html; charset=gb2312 "); $serverName =" 127.0.0.1 "; Database server address $uid = "sa"; Database user Name $pwd = "Password123"; Database Password $connectioninfo = array ("UID" = = $uid, "PWD" = = $pwd, "database" = "test"); $conn = Sqlsrv_connect ($ ServerName, $connectionInfo);//$conn = new PDO ("Sqlsrv:server=127.0.0.1;database=test", "sa", "yyyy1111") or Die ("PDO Connection faild. "); if ($conn = = False) {    echo ' connection failed! ";    Die (Print_r (Sqlsrv_errors (), True));} if ($conn) {echo "Connection succeeded";}

Result: Connection Failed! Array ([0] = = Array ([0] = = 08001 [SQLSTATE] = 08001 [1] = 5 [Code] = 5 [2] = = [Microsoft][sql Serv Er Native Client 11.0]named Pipes provider:could not open a connection to SQL Server [5]. [Message] = [Microsoft][sql Server Native Client 11.0]named Pipes provider:could not open a connection to SQL server [5].) [1] = = Array ([0] = HYT00 [SQLSTATE] = HYT00 [1] = 0 [Code] = 0 [2] = [Microsoft][sql Serve R Native Client 11.0]login timeout expired [message] = [Microsoft][sql Server Native Client 11.0]login Timeout expired ) [2] = = Array ([0] = 08001 [SQLSTATE] = 08001 [1] = 5 [Code] = 5 [2] + = [Microsoft][sql Server Na tive Client 11.0]a network-related or instance-specific error have occurred while establishing A connection to SQL Server. Server is not a found or not accessible. Check If instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL ServeR Books Online. [Message] = [Microsoft][sql Server Native Client 11.0]a network-related or instance-specific error have occurred while Establishing a connection to SQL Server. Server is not a found or not accessible. Check If instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online. ) )

1. Run SQL Server Configuration Manager: SQL Server Config Manager, open protocol protocols
Allow Named pipes "Named Pipes" and "TCP/IP"
2. Right click on "TCP/IP" to open the Properties property tag "IP Addresses"
3. Fill in TCP dynamic port "TCP Ports" 1433
4. Restart SQL Server

The server instance writes this:
$serverName = "127.0.01\sqlexpress, 1542"; 1542 can not write, default is 1433;

Thank you, two brothers.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.