Never give up - LEO

人 只有在合适的地方 才能体现出最大的价值
  博客园  :: 首页  :: 联系 :: 订阅 订阅  :: 管理

Cannot connect PHP 5.2.9 to SQL Server 2008

Posted on 2010-06-11 11:45  lizhiwen  阅读(726)  评论(0编辑  收藏  举报
I have a strange problem while connecting to MSSQL 2008 with php.
First I wrote the code myself and I got error message:

Warning: mssql_connect() [function.mssql-connect]: Unable to connect to server: 172.16.2.12:1433 in C:\xampp\htdocs\test.php on line 19
After googleing for a few hours I found out that with the 2005 version there is some problems with ntwdblib.dll but I cannot find any bugs/problems for this with php 5.2.9 and SQL 2008.

Then I used the code from the Microsoft site:

<?php

// Server in the this format: <computer>\<instance name> or

// <server>,<port> when using a non default port number

$server = '172.16.2.12,1433';



$link = mssql_connect($server, 'Username', 'Password');



if(!$link)

{

die('Something went wrong while connecting to MSSQL');

}

?>
But still I get the error:
Warning: mssql_connect() [function.mssql-connect]: Unable to connect to server: 172.16.2.12:1433 in C:\xampp\htdocs\test.php on line 19

Yes: The port is 1433;
Yes: The port is open;
Yes: I can connect with telnet;
Yes: The username is correct;
Yes: Also is the password.

Apache server is running local on my workstation.
SQL 2008 is running on remote server (172.17.2.12).

I have no idea what the problem is, and I am sure that the code is correct.

Someone any ideas what this might be?

Thanks in advance;

Lucas Mateijsen

Answers

  • Wednesday, August 12, 2009 3:56 AMNai-dong Jin - MSFTMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Hi,

    I think the ntwdblib.dll you used does not support your current SQLServer version.

    I suggest that you may try the following link and get the latest ntwdblib.dll and overwrite your original one.

    http://www.theregister.co.uk/2008/04/24/database_connection_php/

    Thanks.
    Microsoft Online Community Support Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Wednesday, August 12, 2009 5:09 AMjtoddhanard Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    The ntwdblib.dll has never worked for me with any version of sql server other than 7.0.  There's so many different versions of the library floating around, its hard to know which one works with which server version.   I think most people who use mssql functions in PHP use the FreeTDS libraries for either Unix or Windows.  You can get binaries already made... website is www.freetds.org  I've had success with both 2005 and 2008 sql server.  But, since MS wrote a PHP driver, i've had better luck with it.   Both ntwdblib.dll and the freetds libraries use mssql functions but you can only have one running at a time. 

    Now on a side note, the Microsoft PHP Driver doesn't use the mssql function set, it uses the sqlsrv function set.  So, in order to use the MS PHP driver, you need to use sqlsrv_connect, sqlsrv_query, etc.  You can run a phpinfo() command in order to determine if you have either of those installed. 

    This forum is specifically for the microsoft PHP driver, so if you have problems with the ntwdblib, you might want to check some other websites or forums for assistance.

    Best of luck

All Replies

  • Friday, August 07, 2009 4:41 PMStoyko Kostov - MSFT Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Please don't make duplicate posts.
  • Wednesday, August 12, 2009 3:56 AMNai-dong Jin - MSFTMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Hi,

    I think the ntwdblib.dll you used does not support your current SQLServer version.

    I suggest that you may try the following link and get the latest ntwdblib.dll and overwrite your original one.

    http://www.theregister.co.uk/2008/04/24/database_connection_php/

    Thanks.
    Microsoft Online Community Support Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Wednesday, August 12, 2009 5:09 AMjtoddhanard Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    The ntwdblib.dll has never worked for me with any version of sql server other than 7.0.  There's so many different versions of the library floating around, its hard to know which one works with which server version.   I think most people who use mssql functions in PHP use the FreeTDS libraries for either Unix or Windows.  You can get binaries already made... website is www.freetds.org  I've had success with both 2005 and 2008 sql server.  But, since MS wrote a PHP driver, i've had better luck with it.   Both ntwdblib.dll and the freetds libraries use mssql functions but you can only have one running at a time. 

    Now on a side note, the Microsoft PHP Driver doesn't use the mssql function set, it uses the sqlsrv function set.  So, in order to use the MS PHP driver, you need to use sqlsrv_connect, sqlsrv_query, etc.  You can run a phpinfo() command in order to determine if you have either of those installed. 

    This forum is specifically for the microsoft PHP driver, so if you have problems with the ntwdblib, you might want to check some other websites or forums for assistance.

    Best of luck