Jump to content

Archived

This topic is now archived and is closed to further replies.

benphp

Can't connect to SQL Server using sqlsrv_connect

Recommended Posts

I've been working on this for 8 hours and still cannot get sqlsrv_connect to work. Thank you, Microsoft. My connection strings work for Access DBs and MySQL - but I can't get this to work.

 

For servername, I've tried:

$serverName = "MYSERVER";

$serverName = "(MYSERVER\MYINSTANCE)";

$serverName = "'MYSERVER\MYINSTANCE'";

$serverName = "(MYSERVER)";

 

Setup:

PHP Compiler = MSVC9 (Visual C++ 2008)

C:\Program Files (x86)\PHP\v5.3\php.ini

extension_dir C:\Program Files (x86)\PHP\v5.3\ext\

php_sqlsrv_53_nts_vc9.dll in the extension library.

 

And I'm getting an error from sqlsrv_errors, so it looks like the DLL is working. Pulling my hair out here.

 

Any ideas?

 

$serverName = "MYSERVER\MYINSTANCE";
$connectionInfo = array("UID" => $uid, "PWD" => $pwd, "Database"=>"$dbname");
$conn = sqlsrv_connect( $serverName, $connectionInfo);

if($conn) {
echo "Connection established.\n";
} else {
echo "Connection could not be established.\n";
die( print_r( sqlsrv_errors(), true));
}

sqlsrv_close( $conn);

 

 

Connection could not be established. Array ( [0] => Array ( [0] => 08001  => 08001 [1] => 53  => 53 [2] =>

Named Pipes Provider: Could not open a connection to SQL Server [53].  => [sql Server Native Client 10.0]Named Pipes Provider: Could not open a connection to SQL Server [53]. ) [1] => Array ( [0] => HYT00  => HYT00 [1] => 0  => 0 [2] => [sql Server Native Client 10.0]Login timeout expired  => [sql Server Native Client 10.0]Login timeout expired ) [2] => Array ( [0] => 08001  => 08001 [1] => 53  => 53 [2] => [sql Server Native Client 10.0]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not 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.  => [sql Server Native Client 10.0]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not 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. ) ) 

Share this post


Link to post
Share on other sites

Is the server and instance running? Allowing external connections?

Share this post


Link to post
Share on other sites

The only thing I can see so far is that it is possible you may have to define a port:

$serverName = "serverName\sqlexpress, 1542"; //serverName\instanceName, portNumber (default is 1433)

 

If not,  your microsoft sql server may not be initiated correctly.  I'm not familiar with microsofts sql so I can't say for sure.

 

You do use $serverName = "Myserver\Myinstance"; instead of the other ways you've tried also.

 

So far either your information is wrong or your server isn't set up properly, because your code is correct.

 

Is there any other way to test your microsoft sql connection?

 

 

 

Share this post


Link to post
Share on other sites

I may have fixed the connection - I think my DBA wasn't giving me the right instance name.

 

But now I get:

 

Connection string is not valid

 

using the connection string above.

 

:-\

Share this post


Link to post
Share on other sites

Make sure you double-up the backslash in the SERVER\INSTANCE name...

Share this post


Link to post
Share on other sites

After several hours of pulling hair out with PHP v5.3.8, I gave up and installed v5.2.17, which works with sqlsrv_connect. Version 5.3.8 always compiled "--without-mssql" and I couldn't get it to do otherwise. Here's my install notes for installing on Windows, IIS:

 

Don't install v5.3.x. Use v5.2.17 instead.

 

The latest PHP does not support the orginal php sql driver (php_mssql.dll) they have updated ones you need to find like: 'php_sqlsrv_52_ts_vc6.dll' which is what you have to load as an extension.

 

Install v5.2.17

Install web server IIS FastCGI

 

Download Microsoft Drivers for PHP for SQL Server (SQLSRV20.EXE)  from Microsoft http://www.microsoft.com/download/en/details.aspx?id=20098

 

Install SQLSRV20.EXE - install to your extensions directory ..\php\ext\  - It just copies DLLs there.

 

For version 5.2.17 on Windows running IIS7, it will use non-threaded 5.2 version Compiler v6 files:

php_pdo_sqlsrv_52_nts_vc6.dll

php_sqlsrv_52_nts_vc6.dll

 

---

Go into IIS7 Manager and click PHP Manager. Enable the extensions:

 

php_pdo_sqlsrv_52_nts_vc6.dll

php_sqlsrv_52_nts_vc6.dll

 

This adds extensions to the ini:

[php_PDO_SQLSRV_52_NTS_VC6]
extension=php_pdo_sqlsrv_52_nts_vc6.dll
[php_SQLSRV_52_NTS_VC6]
extension=php_sqlsrv_52_nts_vc6.dll

Note – different versions of the DLL for 5.2.

Connection test:

<?php
print "Connecting...<br /><br />";

$serverName = "MYSERVERNAMEHERE";
$uid = "my_username_here";
$pwd = "my_secret_password";
$dbname = "MyDatabaseName";

$connectionInfo = array( "Database"=>"$dbName", "UID"=>"$uid", "PWD"=>"$pwd");
$conn = sqlsrv_connect( $serverName, $connectionInfo);

if($conn) {
echo "Connection established.\n";
} else {
echo "Connection could not be established.\n";
die( print_r( sqlsrv_errors(), true));
}

sqlsrv_close( $conn);
?>

Share this post


Link to post
Share on other sites

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.