简体   繁体   中英

I'm unable to connect to ms sql server from php

I installed sql server 2008 on my pc and try to connect to it from php code.

$server = "127.0.0.1:1433\MSSQLSERVER";
mssql_connect($server, 'sa', '123456');

and faced with this error.

Unable to connect to server: 127.0.0.1:1433\\MSSQLSERVER

please help me.

Are you using an named instance MSSQLSERVER? SQLExpress uses the named instance MSSQLSERVER by default. SQL Server does not do this, it's default is to not use named instances.

The default database in SQL will operate on port 1433 unless you specify otherwise when you set it up. By default most applications will assume this is the port if you don't specify and attempt to connect to it. The purpose of named instances is that each DB instance actually operates on it's own port and so if you specify an instance, the default instance running on 1433 will respond with a "no dude the DB you want is running on this port."

Have you tried just 127.0.0.1:1433?

[UPDATE]

Last time I played with PHP and SQL Server I used odbc_connect and not mssql_connect and I setup a DSN entry with connection parameters. This was the code I used.

$dsn="MyDatabase";
$username="phpUser";
$password="whatever";

$sqlconnect=odbc_connect($dsn, $username, $password);

$sqlquery="SELECT * FROM Sample;";

$results= odbc_exec($sqlconnect,$sqlquery);

[UPDATE2]

Microsoft released Microsoft Drivers for PHP for SQL Server and there's a new way to connect to MSSQL.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM