Jump to content

Warning: mssql_connect()

Recommended Posts

I have a new install of PHP with Apache 2.2 on a Windows XP box and am getting the following error when using this line of PHP:


mssql_connect($server,$username,$password) or die ("Could not connect");


Warning: mssql_connect() [function.mssql-connect]: Unable to connect to server: "servername" in "directory" on line 226.


I have the latest version of ntwdblib.dll from http://www.helpspot.com/helpdesk/index.php?pg=kb.page&id=13


The following returns



if (function_exists('mssql_fetch_row')) {

echo "MSSQL functions are available.<br />\n";

} else {

echo "MSSQL functions are not available.<br />\n";









MSSQL functions are available.


Multiple locations is odd but all relevant files (identical) are in both locations

Configuration File (php.ini) Path C:\WINDOWS

Loaded Configuration File C:\php\php.ini

extension_dir C:\php\ext C:\php\ext


Uncommented in php.ini (both C:\Windows and C:\php



Tested connection using MSSQL Management Studio, login/pwd works.


Have tested this with 2008 SQL Express and 2005 SQL Full, neither works. I do have this install working with MySQL.


Any ideas?

Link to comment
Share on other sites

Have you configured SQL Server to allow remote connections? If you have, can you get more detailed error info?


BTW, since you are running PHP on Windows, I'd suggest you use the Microsoft-built driver (available here: http://www.microsoft.com/downloads/details.aspx?displaylang=en&FamilyID=ccdf728b-1ea0-48a8-a84a-5052214caad9 , or via the Web Platform Installer: http://www.microsoft.com/web/downloads/platform.aspx ). It does not rely on the depricated DBLib technology. If you are interested, more info here: http://blogs.msdn.com/b/brian_swan/archive/2010/03/08/mssql-vs-sqlsrv-what-s-the-difference-part-1.aspx and




Link to comment
Share on other sites

I was able to connect to the DB. Thanks for the info, fun read


For anyone interested, I had to add the extensions to my C:\php\ext directory and the php.ini file, from there just use the new functions as described in




and if you're using mixed authentication here is the format, as this function uses Windows authentication by default.


$connectionInfo = array( "UID"=>"myPassword","PWD"=>"myPassword");

$conn = sqlsrv_connect( $serverName, $connectionInfo);

Link to comment
Share on other sites

Correct, I connected with php_sqlsrv.dll . I am running into an issue with sqlsrv_fetch though.  I'm passing it a valid query in the form of




but am receiving the following error in the code segment



while ($i <= $result+1) {

if( sqlsrv_fetch( $result ) === false )


echo "Error in retrieving row.\n";

die( print_r( sqlsrv_errors(), true));



Connection established. Error in retrieving row. Array ( [0) => Array ( [0) => IMSSP [sqlSTATE) => IMSSP [1) => -28 [code) => -28 [2) => The active result for the query contains no fields. [message) => The active result for the query contains no fields. ) )


Ignore incorrect brackets/parentheses combination. [] was messing up the post so I changed it to [)


But yea, the sqlsrv driver works great so far. A bit of an adjustment since most examples are mssql related.

Link to comment
Share on other sites

SQLSTATE: IMSSP code: -28 message: The active result for the query contains no fields.


Looks like it matches





For errors that originate from the ODBC driver, the SQLSTATE returned by ODBC. For information about SQLSTATE values for ODBC, see ODBC Error Codes.

For errors that originate from the SQL Server Driver for PHP, a SQLSTATE of IMSSP.


For warnings that originate from the SQL Server Driver for PHP, a SQLSTATE of 01SSP.






but I can't seem to find what the error code -28 means for this driver. (Edit:: other than the brief description of course)



Link to comment
Share on other sites

I think your query is returning 2 result sets: one for the "USE database" statement, and one for the "SELECT ..." statement. The first result set has no fields (obviously). Try calling sqlsrv_next_result($stmt) after you execute the query and before you start iterating through rows. Let me know how that works.



Link to comment
Share on other sites

  • 1 year later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • 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.