Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP with SQL Server 2005+

Tags:

php

sql-server

Currently we have a hybrid ASP/PHP setup connecting to a SQL Server 2005 database. But all the query work is done on the client side, I'd like to move some of this to PHP.

What driver and/or connection string is needed to connect to Sql Svr and what is the syntax to use in PHP?


Update: OK so I was definitely trying to avoid using anything to do with copying DLLs etc. I'll look into the SQL2K5PHP driver (thanks Vincent). @jcarrascal for the sake of clarity, by "client side" I mean our application is an internal web app that runs as an HTA, with all queries done via javascript calls to an ASP which actually submits the DB request.

like image 613
Scott Bennett-McLeish Avatar asked Sep 20 '26 00:09

Scott Bennett-McLeish


2 Answers

You have two options:

1) php_mssql extension : If you'd like something that has the same API mysql and mysqli has, then use the php_mssql extension. But there is a catch, the bundled ntwdblib.dll file with PHP is not working. You have to find this file from a SQL Server 2000 installation or you can find it on the Internet. This API is supposedly not very reliable but I have been using it without problem for about one year.

http://ca.php.net/mssql

2) Microsoft SQL Server 2005 PHP Driver : If you'd like something more modern but which does not have the same API and is missing some important functions (mssql_num_rows). The big plus is that it is supported by Microsoft and is likely to work with a future version.

http://msdn.microsoft.com/en-us/data/cc299381.aspx

like image 144
Vincent Avatar answered Sep 22 '26 14:09

Vincent


Just use the mssql_connect() function like this:

$conn = mssql_connect('localhost', 'sa' , '123456')
    or die('Can\'t connect.');
mssql_select_db('database', $conn)
    or die('Can\'t select the database');

Functions relating to SQL Server are defined in the PHP manual for the MSSQL driver.

One question though, "all the query work is done on the client side" WTF? :D

like image 42
Julio César Avatar answered Sep 22 '26 15:09

Julio César



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!