mssql_connect

(PHP 4, PHP 5, PECL odbtp >= 1.1.1)

mssql_connectOpen MS SQL server connection

Warning

This function was REMOVED in PHP 7.0.0.

Alternatives to this function include:

Description

mssql_connect ([ string $servername [, string $username [, string $password [, bool $new_link = FALSE ]]]] ) : resource

mssql_connect() establishes a connection to a MS SQL server.

The link to the server will be closed as soon as the execution of the script ends, unless it's closed earlier by explicitly calling mssql_close().

Parameters

servername

The MS SQL server. It can also include a port number, e.g. hostname:port (Linux), or hostname,port (Windows).

username

The username.

password

The password.

new_link

If a second call is made to mssql_connect() with the same arguments, no new link will be established, but instead, the link identifier of the already opened link will be returned. This parameter modifies this behavior and makes mssql_connect() always open a new link, even if mssql_connect() was called before with the same parameters.

Return Values

Returns a MS SQL link identifier on success, or FALSE on error.

Changelog

Version Description
5.1.0 The new_link parameter was added

Examples

Example #1 mssql_connect() example

<?php
// Server in the this format: <computer>\<instance name> or 
// <server>,<port> when using a non default port number
$server 'KALLESPC\SQLEXPRESS';

// Connect to MSSQL
$link mssql_connect($server'sa''phpfi');

if (!
$link) {
    die(
'Something went wrong while connecting to MSSQL');
}
?>

See Also

add a note add a note

User Contributed Notes 40 notes

up
11
benjamin at mysteriousquilt dot se
13 years ago
If someone encounters the interesting problem in which PHP can connect to a MSSQL server from the command line but not when running as an Apache module: SELinux prevents Apache (and therefore all Apache modules) from making remote connections by default.

This solved the problem in CentOS:
# setsebool -P httpd_can_network_connect=1
up
8
mckoin at example dot com
19 years ago
If you use PHP on Windows with Apache as a web server, you may get problems with authentication to MS SQL Server even when you supply all valid credentials.

Check your php.ini file:
; Use NT authentication when connecting to the server
mssql.secure_connection = On

If you have secure_connection = On, make sure that you provide valid credentials in the properties for Apache service in the System Services box. Then you should not send DB username and password from your script to MSSQL Server.

If you want to use specific credentials from a PHP script, then set mssql.secure_connection = Off in your php.ini
up
5
parikhnirav dot aits at gmail dot com
11 years ago
Cent OS - Remote SQL server connection
$link = mssql_connect(72.xx.xxx.xxx, username, password);

If you are not getting success, please check

There is certainly a php-mssql package available in the extras repository so just a `yum install php-mssql` should sort that out - it pulls in freetds as a dependency too. If you already have those installed but it doesn't seem to be connecting, the one possibility is that you are being blocked by SELinux. To check that run

# getsebool -a | grep httpd_can_network_connect
httpd_can_network_connect --> on
httpd_can_network_connect_db --> on

and to enable them if they are not on, do

setsebool -P httpd_can_network_connect 1
setsebool -P httpd_can_network_connect_db 1

taken from
https://www.centos.org/modules/newbb/print.php?form=1&topic_id=29646&forum=38&order=DESC&start=0
up
0
herrwil at gmail dot com
12 years ago
The following method: server\instance
does'nt work on unix systems...

An instance is nothing more than specific port address different than 1433... so just discover the port on mssql server and then try to connect using:

ip:port

In unix the port is specified by : not by ,
up
0
wwang at moultonlogistics dot com
15 years ago
For anyone who have to connect to crappy SQL SERVER with \\computer_name\instance_name and failed to connect. There are couple of thing you need do.

1). There's no needs to install SQL client software in your app server. This would keep your server clean.
2). run cliconfg.exe setup alias in case you needed
3). last and most important, do NOT use ntwdblib.dll came with PHP 4.4.x. It's not most current. The version I'm using is: 8.00.194 with 274,489 Bytes. And make sure you put this file in your system32 folder.
up
0
getzeroedin at hotmail dot com
16 years ago
For those trying to connect PHP 5.2 to a 2005 Microsoft SQL Server Analysis Services (SSAS) cube and execute MDX queries, you have to establish a link via a trusted connection from the Database Service to SSAS using a stored procedure, then execute the stored procedure via PHP.

Here is an example stored procedure that retrieves records from the Adventure Works sample cube that ships with SSAS.  

From SQL Server Query Analyzer, you could test it as: 

exec testMDX

From PHP, you would execute something like the following:

$resultset = mssql_query("exec testMDX",$res_id);

then loop thorugh the result set.

-- STORED PROCEDURE BEGIN

set ANSI_NULLS ON -- Must be enabled at time Stored Proc is created
set QUOTED_IDENTIFIER ON -- Must be enabled at time Stored Proc is created
GO

Create Procedure [dbo].[testMDX]
as
BEGIN
SET ANSI_WARNINGS ON:  -- Must be enabled
SET ANSI_NULLS ON; -- Must be enabled

Declare
@SQL varchar(1200),  -- Variable to hold SQL query
@MDX varchar (1000), -- Variable to hold MDX query
;

-- Establish a link to Analysis Server
exec sp_addlinkedserver
@server='linked_olap', -- Alias used to reference the link
@srvproduct='', -- Not used
@provider='MSOLAP.3', -- OLAP driver
@datasrc='servername', -- Database server name
@catalog='Adventure Works DW Standard Edition' -- Database name
;

-- Analysis Server requires a TRUSTED connection
exec sp_addlinkedsrvlogin
@rmtsrvname = 'linked_olap', -- Alias used to reference the link
@useself = 'false', -- Use own credentials
@locallogin = NULL, -- Apply to all local logins
@rmtuser = 'domain\username', -- Remote user name
@rmtpassword = 'xyz123' -- Remote user password
;

-- Create a temporary table that will be used to hold the MDX output
create table #temp_table (column1 text null, column2 text null);

-- Setup a string to hold the MDX so that the precompiler does not try to validate the syntax
SET @MDX = 'SELECT [Product].[Category].members ON ROWS,
                    {Measures.[Internet Order Count]} ON COLUMNS  
                    FROM [Adventure Works]    ' ;

-- Setup a string to insert the MDX results into the temporary table
SET @SQL = 'Insert into #temp_table SELECT * FROM OpenQuery(linked_olap,'''+@MDX+''')';

-- Execute the SQL and remote MDX query
EXEC (@SQL) ;

-- Select the results from the temporary table to return to the calling program
Select column1, column2 from #temp_table ;

-- Drop the temporary table
drop table #temp_table;

-- Release the TRUSTED connection
exec sp_droplinkedsrvlogin 'linked_olap', NULL ;

-- Release the link to the Analysis Server
exec sp_dropserver 'linked_olap' ;

END

-- STORED PROCEDURE END
up
-1
Sarke
14 years ago
By far the easiest way that I've found to connect to a SQL server is to route it through a ODBC connection.

1. Administrative Tools -> Data Sources (ODBC)
2. System DNS
3. Add... "SQL Server"

Then just use odbc_connect("sql_test", "user", "pass") instead.
up
-1
nospam_bg at pumkinhed dot com
19 years ago
On freebsd I edited the Makefile in /usr/ports/lang/php5 and added  --with-mssql=/usr/local to the CONFIGURE_ARGS= section (ensure to put a \ in front of the previous line), then it will use tds.h correctly....  hth
up
-1
Anonymous
15 years ago
Just a remind: Although it is not present here in this page, the parameter **$new_link** holds for mssql_connect() and mssql_pconnect(). The correspondent mysql functions documentation describes it though.
up
-1
rcgandara at gmail dot com
16 years ago
If your are trying to connect to a MSSQLServer 8 and got this error: Unabled to connect to: [SERVER_NAME]
and u r using Server authentication, make sure 2 set up mssql.secure_connection to Off in da php.ini

RCGP

Making this world better everyday... peace out
up
-1
edortizq at gmail dot com
16 years ago
For connecting Linux + Apache + SQLEXPRESS 2005 take care about it:

- DonĀ“t use the standard MS-SQL port (1433), use the MS-SQL dinamic port under SQL Server Configuration Manager -> SQL Express Protocols -> TCP/IP properties -> IP Adresses -> IPAll

- You can do a direct connection (without FreeTDS) using the following statemt:
  $db=mssql_connect('192.168.xxx.xxx:1541','usrxxxx','pwdxxxx');

-You can use FreeTDS configuring the freetds.conf file as follow:
  [connect2k5]
  host = 192.168.xxx.xxx
  port = 1541
  tds version = 8.0
With the following PHP statement:
  $db=mssql_connect('connect2k5','usrxxxx','pwdxxxx');
up
-1
Marcel Laverdet
19 years ago
I was having problems connecting to a SQL cluster with mssql_connect, but any other program could connect just fine. I ended up using a PHP ADODB driver to work around the connectivity problem I was having. Later, however, I discovered my problem was a mismatched name in the server table.

To see if you have this problem, "SELECT * FROM [master].[sysservers]". If the srvname, and datasource don't match the cluster's virtual SQL name... then you will have a problem. My virtual SQL server's name was 'SQL-SERV', however in the sysservers table it was listed as 'SQLSERV'.

To remedy this problem, simply issue the following queries: "EXECUTE sp_dropserver wrongName" and then "EXECUTE sp_addserver rightName, 'local'". After running these 2 procedures, restart the server and PHP should connect just fine using the native libraries.

If it still doesn't connect, make sure you update the ntwdblib.dll file as explained in prior comments.
up
-1
bass28
19 years ago
When connecting to a SQL 2000 DB from an Apache/PHP setup on a Windows box you must connect using a SQL user login (type=standard).  If you add Windows users or groups to SQL security logins and try connecting using that login the connection will fail.  If the connection works for SA but not for your login, this may be your answer (it was for me!).
up
-1
kryltsov at yahoo dot com
19 years ago
If you connect to MSSQL and  get error 515 (INSERT NULL INTO NOT NULL VALUE).
Execute following statement after connection to DB.
"set ANSI_NULL_DFLT_ON ON". There are lots of code arround where SP creates, for example, temp tables. There is no specification 'NOT NULL' on columns. Many people beleive that it means that nulls will be allowed.
After 5 days of debugging one of the software monsters SQL code I found that PHP connects to MSSQL (at least on my server) without setting "ANSI_NULL_DFLT_ON" which caused SP fired by insert trigger failed with 515 error because temp tables had been created with all columns 'NOT NULL'.
QA and from ASP everything was working just fine.

After I set ANSI_NULL_DFLT_ON to ON. Everything started working fine.
Hope it wil help.
up
-2
j3d
19 years ago
After struggling for 6 hours trying to fix the dreaded "Not associated with a trusted SQL Server connection." error I have finally solved it after following the suggestions presented here:
http://support.microsoft.com/default.aspx?scid=kb;en-us;839569

<sarcasm> Who would have thought that microsoft's article would be useful </sarcasm>
up
-2
hao dot wang at outsourcing dot org
17 years ago
For those poor people (like myself) who have to use MS SH*T SQL 2005 and cannot connect it.

I have to use sql server 2005 (another piece of sh*t from MS) at work. It takes me hours to connect this garbage. I end up find I have to enable TCP/IP under "Protocols for MSSQLSERVER". To do this, you need go to
START >> PROGRAMS >> MS Sql server 2005 >> Configuration Tools >> Sql Server configuration Manger

Click "+" sign next to "Sql server 2005 network configuration" then
click to highlight "Protocols for MSSQLSERVER" then
in right panel right click "TCP/IP" and enable it.

DO NOT for get restart you sql server service and pray to dear God your company will dump MS one day.

DO NOT install "ActiveScript engine" as MS suggested. You don't want extra trouble and junk.
up
-2
Robert at realjax dot com
19 years ago
A view that worked fine when queried from any other environment was giving me strange results when queried from PHP.  The problem is that the MS SQL database settings are not set to the ANSI defaults as when connecting through Microsoft products.  The setting CONCAT_NULL_YIELDS_NULL defaults to ON when connecting with ODBC or SQL Query Analyzer, which complies with the ANSI standard.  However, this defaults to OFF when connecting through PHP.  There are many other settings which may also need to be explicitly set.
<?php
mssql_query
('SET CONCAT_NULL_YIELDS_NULL ON', $hd);
?>
up
-2
s dot westermayer at nospam-netnnet dot de
22 years ago
SQL Server Authentication Mode must be set to "SQL Server and Windows" or your SQL Server will not accept a connection and diplay:
Warning: MS SQL message: Login failed for user 'myuser'. Reason: Not associated with a trusted SQL Server connection. (severity 14) in...
up
-3
mworku at securiant dot com
20 years ago
It looks like by default libphp5 ignores freetd's conf file if you
specify your SQL server's ip address within the mssql_connect() function.

If you need to utilize freetds.conf file for connection parameters use the putenv function as follows

<?php
  putenv
('FREETDSCONF='path to your freetds conf. file')

  /*
    SQL CODE
    mssql_connect('
MSSQL_2000, 'user' , 'pwd');
    ....
    ....
  */
?>

All connection parameters should now be read from the conf file. Also don't enable logging unless you're debugging, performace will suffer.

/* freetds.conf
[MSSQL_2000]
    host = 192.168.10.10
    port = 1433
    tds version = 8.0
;    dump file = /etc/freetds/log/freetds.log
;            dump file append = yes
;    debug level = 99
*/
up
-4
ben at adworkshop dot com
13 years ago
Connecting to a named instance of MSSQL.
Our environment is a Windows 2003 server.
Connecting to a dedicated MSSQL server running two instances of SQL SERVER 2005. The default instance and then a second named instance.
My issue was in finding the syntax to properly identify the port of the named instance server.
I found the answer finally in this thread, where a comma is used to separate the IP and PORT number.

So, the code:

mssql_connect('192.0.0.0,3456','USERNAME','PASSWORD');

In this case we used the internal IP of the server, followed by the static port number assigned to the named instance, in this example 3456. The port number can be dynamic or static, obviously choosing static is the route to take.

Hope this helps someone!
up
-3
pgeier AT international-capital DOTcom
14 years ago
If you have an application which requires MSSQL as interface (and not the new SQLSRV) you have to look for an alternative method. The old (now rightly unsupported) way from Microsoft does not work with PHP 5.3.
After spending several days and nights with trying to connect
here is an easy solution which worked great with all applicatins expecting mssql: FreeTDS for Windows. Here is a great how to with installer links on the Moodle site: http://docs.moodle.org/en/Installing_MSSQL_for_PHP

Couple of hints:
- Make sure you have set up a user with "SQL SERVER authentication" who has rights to connect.
- Also make sure after you install and still have problems to run PHPinfo and check that you find the below:

PHPINFO: mssql
MSSQL Support enabled
...
Library version  FreeTDS 

That works perfect for me on Windows 2003 with IIS6 and PHP 5.3 (non threadsafe, VC9).
Hope this is helpful
up
-2
Iuri Jacob
15 years ago
I was trying to connect to a remote SQL2000 server (located on Internet, not on the local network), then php always returned an "Unable to connect" error or something.

After waste a lot of time, by searching among foruns, blogs and logs, I finally discovered  the problem. I was monitoring the TCP connections between the server and my machine when I realized that it wasn't been done. For some reason, my Apache 2.2 was "denying" php module from doing remote connections.

So I changed the php configuration on apache to CGI mode rather than as module mode. Well, it worked fine since that.

In despite of security holes that it involves, it was the only way I've found to make that remote connection works between Apache2.2/PHP5 and MSSQL Server 2000.

Strangely, connections inside local network worked in any case (in CGI or module mode).
up
-2
b56w
15 years ago
After a great deal of testing various things I finally found a solution for connecting to a named instance of MS SQL Express.  I first had to download version 200.80.194.0 of ntwdblib.dll and overwrite the existing version in c:\PHP.  I then tried an instance name that was posted by "chop_01 at yahoo dot com" in this thread.  The instance name is ".\SQLEXPRESS".

For example:

<?php
  $dbconnect
= mssql_connect(".\SQLEXPRESS", $dbUser, $dbPass);
?>
up
-2
munckfish.net
18 years ago
Further to the following posts on Windows 2003 + PHP5 + SQL Server Express.

http://php.net/manual/en/function.mssql-connect.php#58787
http://uk2.php.net/manual/en/function.mssql-connect.php#61971

I ran into another problem when I updated the version of ntwdblib.dll. I picked up version 2000.80.2039.0 from an SQL Server 2000 installation. However, when I replaced the PHP 5 provided version with this one and restarted IIS, phpinfo() showed that the mssql extension wasn't loading anymore. I couldn't see any errors reported in browser nor the system error logs.

By chance I decided to run 'php -i' on the command line. This was lucky because running it this way a system error dialog popped up a warning about a missing DLL dependency.

The problem was that the new ntwdblib.dll had a dependency on MSVCR71.DLL which couldn't be found in IIS' path. I did a search for msvcr*.dll in C:\\WINDOWS and found a copy of this DLL in C:\\WINDOWS\\Microsoft.NET\\Framework\\v1.1.4322\\. I copied it into my PHP install dir and restarted IIS. Then when I ran phpinfo() the extension showed up again.
up
-3
yurtesen at ispro dot net dot tr
19 years ago
If you are using FreeBSD you shouldnt forget to copy freetds.conf.dist to freetds.conf and edit. Otherwise you will get:
PHP Warning:  mssql_connect(): Unable to connect to server:  BLAH in
up
-2
dale dot liszka at gmail dot com
19 years ago
If you are trying to use Sybase and MSSQL on the same WIN32 box you will probably have headaches.  There are multiple declaration errors when you enable php_sybase_ct.dll and php_mssql.dll in the php.ini at the same time.

The specific error for all functions starts like this:
PHP Warning: Function registration failed - duplicate name - mssql_connect

There is a way around this! On a windows box I have done this successfully several times.

1. Make a backup copy of php_sybase_ct.dll (never hurts!)
2. Open php_sybase_ct.dll in a HEX editor (fhred is a nice free one http://www.kibria.de/frhed.html )
3. Replace all instances of 'mssql' with 'sysql'.  It doesn't have to be 'sysql', just anything but 'mssql' or 'sybase'.
4. Save the file.  (Alternately you could save as something like php_sybase_ct_modified.dll and cite that in your php.ini)

All the constructs are still declared for the sybase_* functions and now you can use MSSQL and Sybase instead of switching php.ini files, etc.  I have had this work under php 4.3+ and php 5.0.5.

PS. I decided to place this under mssql_connect because its the first error thrown in this case.  I believe it should make the information easier to find via google/search/browsing.
up
-5
kamikazie at comcast dot net
18 years ago
Ok, so after many many many hours of searching (somewhere around the ballpark of 2 weeks, on and off) and reading these pages, I've found the answer to my unspoken question (although there was much cursing).

Background:

A Windows Server 2003 box running as a router and a webserver using IIS6 and the latest PHP 5 as of 23 Sep 2006.

A Windows Server 2003 box running SQL 2000 Enterprise with one instance

Using a named pipe, the code to connect was:
"$connection = mssql_connect("\\.\pipe\MSSQL$instance\sql\query", "user", "pass") or die("Message"); "

I recieved the error that $instance was not a defined variable, so I changed it to
"\\.\pipe\MSSQLinstance\sql\query"
both on the connection string and the SQL server

When passed on to the server, I still recieved connection errors, so I changed it again, this time to:
"$connection = mssql_connect("\\\\.\pipe\\MSSQLinstance\\sql\\query", "user", "pass") or die("Message"); "

With this new code, I recieved a successful connection, but a bad login saying that my IUSR_ account did not have permissions on the database, although I used a separate username in the connection string.

Using SQL, I setup windows authentication for the IUSR_ account and I was able to establish a connection and successfully run a query.

I still can't find out why it was trying to pass the IUSR_ account as the login though.  If anyone has an answer drop me a line.

Now using windows groups it should be easy to create a login page that uses groups to connect to databases rather than setting up each user individually.

I just wanted to share with everyone my long awaited success story.
up
-3
Colin
17 years ago
I had lots of issues with trying to get SQL Server '05 (NOT Express) to work with PHP 5.1.2 on Windows Server '03 SP2 running IIS 6. This is what finally worked:
- Uncomment the php_mssql.dll line in php.ini
- Replace the ntwdblib.dll file in /php/ext/ with the (smaller) downloadable file from webzila.com
- Set SQL Server to mix-mode authentication ("Windows and SQL Server Auth")
- Create IUSR_<computer_name> account in SQL Server using Windows authentication
- Grant read/write access to the IUSR_<computer_name> in SQL Server
- Create SQL auth account using SQL authentication
- Grant read/write access to the SQL auth account in SQL Server
- Restart the system

I'd imagine that creating a way for IUSR to get into the SQL Server is NOT a good idea, but this is the only way I could get it to work. Hopefully, a future update to SQL Server will fix this.
up
-2
bartlewis at gmail dot com
19 years ago
I am yet another person who had problems getting php[5] to play nicely with SQL Server 2000 [MSDE] on WinXP SP2 w/IIS. Following the notes on this page from previous users did get me closer, but I still could not connect. I was getting an error that said, in essence:

"MACHINENAME\IUSR_MACHINENAME" could not connect to the server.

Through some painstaking googeling I was able to resolve the problem by following this MSKB article:
http://support.microsoft.com/default.aspx?scid=kb;en-us;319930

It seems that my default install of MSDE did NOT "Enable Mixed Mode Authentication" which is required for this kind of connection. To make matters worse, the only way to change this with MSDE is to go mucking with the registry (which always freaks me out a bit). The above article explains how to do this.
up
-2
cbunk at arescorporation dot com
19 years ago
I was having problems connecting a IIS6 Webserver on a 2003 windows box to a MSSQL Installation running windows 2000.  After suspecting ntwdblib.dll I tried updating the file but with no luck.  Finally I figured out the problem by noticing that in addition to a copy of the file living in the windows directory there was one in the PHP directory as well (from the zip package).  Although I had an up to date version of the file in my windows directory the one in the php directory was getting read instead.  I simply removed the file from my php installation directory and all worked well.
up
-2
chrisputnam at gmail dot com
20 years ago
If you're having trouble getting PHP to connect to a SQL 2000 Server (the MSDE, specifically), try running the Server Network Client usually located at C:\Program Files\Microsoft SQL Server\80\Tools\Binn\SVRNETCN.exe

Disable all protocols but Named Pipes. Then Apply your changes, click OK, and restart the SQL service with these commands at a command prompt:

net stop mssqlserver [if it says the agent will stop too, let it happen]
net start mssqlserver
net start sqlserveragent

That fixed it all for me.

Also note that named pipes has a huge performance difference against TCP/IP. Named pipes are a tremendous amount faster.

I have been working on this for two days now and I hope someone else stumbles across my solution!
up
-2
bianco at bluetone dot com
20 years ago
Little idea about 255 characters limitation. :)

In tables I'm using column type "varchar" or "nvarchar" instead "text". And also I use for example varchar(8000). And there is "a bug". :) PHP will read this column only as varchar(255). So how to read more then 255 chars from varchar(8000)? It is very easy, use CAST to convert varchar type to text type.

select CAST(Comment as TEXT) from Job

PHP will read it. Check php.ini section [MSSQL] and set maximum text length. Check parameters

mssql.textlimit = 4096
mssql.textsize = 4096

Maximum length is 2147483647

Bye
up
-2
nuying117 at 163 dot com
20 years ago
I came across the same question as jack,but there was no effect when i use his method.and then ,I created a user named IUSER_YOUR_COMPUTER_NAME which is the same as the user who start the IIS process,it works!
up
-5
dju @ work
14 years ago
On a windows 2000 server, i have php 2.6 and apache
It's been 2 days i'm trying to connect it to mssql server 2K5.
i tried everything, of course getting the last version of the sql library (ntwdblib.dll), check the permissions, the dll version etc...  but no way....
Finally, i installed the last version of MDAC 2.8
(there's an old one on win2000, even with all the updates...)
And now it works perfectly :-))
up
-5
Micio2
14 years ago
When you try to connect to SQL Server 2008 Express, you always have to name the instance you are trying to connect to, even if you installed SQL2008express as "Default" instance instead of named instance. If your instance is SQLExpress and server name is myServer, the server parameter must be: "myServer\SQLExpress". If you installed Default instance of Standard (not express) version of SQL2008 instead, you don't need to name the instance. To connect using TCP/IP, you need also to check server network configuration, enable TCP/IP (listen all: yes), then go to addresses tab and setup port 1433 in all addresses you want the server listen on (by default, port is blank and server don't listen). Libraries involved in TCP/IP are ntwdblib.dll and dbnetlib.dll. Have fun!
up
-4
pesadelo at death dot fm
15 years ago
Just a remind: Although it is not present here in this page, the parameter **$new_link** holds for mssql_connect() and mssql_pconnect(). The correspondent mysql functions documentation describes it though.
up
-4
simonhukin at gmail dot com
16 years ago
After a couple of days (seems to be the common theme) trying to get my code to connect to our new clustered 2005 SQL server instance. I finally did it. :-)

For the following setup:

PHP 5.2.4 (hosted on a Windows Server 2003 box)
connecting to MSSQL 2005 in a clustered environment
(was already connecting to MSSQL 2000 with no problems)

I was about to get the client tools installed on the server, but this would have required a trip down to the data center by our IT guys (last resort - well actually last resort was to write a data access service in Java, which was connecting fine).

Anyway, after making sure all the ntwdblib.dll were of the 2000.80.194.0 version. It was still not working.

The thing which made it work was simply to specify the port.

yes after 2days of looking into alternatives (incidently freetds - a windows compiled version, was working very intermittently) I found all I had to do was specify the port!

I guess this is because I needed to connect via TCP/IP, our network admin didn't want named pipes used (as it's non-standard). Although, named pipes were enabled anyway. Specifying the port must have made the default behavour to connect via TCP/IP. Also the port was '1433' which I assumed was default anyway, hence hadn't specified before.
up
-3
me at pazuzusfate dot de
14 years ago
I didn't get it working with PHP 5.2.9 and MS SQL 2008, so I used this article http://msdn.microsoft.com/en-us/library/cc793139(SQL.90).aspx and with SQL login (not Windows authentication!) it works fine!
up
-3
Anonymous
16 years ago
This might be obvious to some, but here is a quick tidbit that might save you some time if you're using FreeTDS in Linux:

Be sure that you have these two lines in freetds.conf:
dump file = /tmp/freetds.log
dump file append = yes

so you can tail -f it in the background of debugging the problem.  This helped me find my issue on on CentOS Linux: 

1) tsql test works
2) php-mssql connection in php also works WHEN RUN FROM THE SHELL
3) running PHP through apache does NOT work.

my /tmp/freetds.log file told me:
net.c:168:Connecting to MYDBSERVER port MYDBPORT
net.c:237:tds_open_socket: MYDBSERVER:MYDBPORT: Permission denied

and the answer was my firewall/SELinux was denying the Apache processes access to connect to the remote MSSQL DB port, but my shell accounts were fine.
up
-5
Filipus
21 years ago
Linux MDK 9.0, PHP 4.3.0 MSSQL 2000 on remote host
We used freetds (www.freetds.org) compiled with parameter:
./configure --prefix=/usr/local/freetds
PHP was compiled with parameter:
./configure --with-mssql=/usr/local/freetds [and some others]

We added our section in /usr/local/freetds/freetds.conf
----------------------------------
[MyServer70]
host = 172.16.20.81
port = 1433
tds version = 7.0
----------------------------------
in mssql_connect() you have to provide as a hostname the name you put between [] brackets in freetds.conf OR specify host:port

$connection = mssql_connect("MyServer70", "d", "d");
or
$connection = mssql_connect("172.16.20.81:1433", "d", "d");

Remember to specify port!
To Top