PHP Code to access SQL SERVER 2008

Leave a comment (2) Go to comments

This article provides instructions on how to enable the SQL Server modules in your PHP installation and how to use SQL Server with the DB package.

You’ll need to enable the sybase or mssql modules in your PHP installation. Most Linux and the common BSD distributions make it an option when installing PHP. If yours doesn’t, the steps to get it into place are simple.

  1. The first dependency is freetds, the library that enables access to Sybase and SQL Server databases. You can download it from It installs like most open source packages, with the cycle of configure, make, make install.
  2. Next you need to build your PHP installation so that it knows about freetds. That’s as simple as adding –with-mssql to the configure switches in most places. Freetds installs by default in /usr/local, so you’ll probably need to modify the switch slightly to read –with-mssql=/usr/local
  3. Now copy your new binaries into place and activate them in your php.ini file. There is already an entry in the default INI file to enable mssql, and it just needs to have the comments removed. It’s usually necessary to restart your web server after adding the module.
  4. You can verify that the module is installed and happy by checking the php_info() command. In the list of modules should be the mssql module. If you’re on a UNIX system, especially FreeBSD or OpenBSD, the mssql module often doesn’t appear even when you do have access to it. If the Sybase module is active, mssql is working too. Explicitly specifying the –with-mssql option to configure will usually force php_info() to divulge the mssql module.
  5. The PEAR DB package is incredibly useful for the aspiring cross-platform developer. Since using PHP with MS SQL is not only cross platform but cross culture, it makes a lot of sense to use it when you’re looking at a PHP/MS SQL combination. DB is one of the core modules included in PEAR, so if you’ve already installed PEAR you should have DB available.

If you haven’t installed PEAR you can get it from Install it in a common folder on your server (or under your server account) and make sure that it’s included in the PHP Include path. If you don’t have access to the php.ini file, you can just copy the PEAR distribution into the root of your application, although this gets a little cumbersome.

Using DB

DB is a class hierarchy that lets you use different database engines without changing your PHP code. It makes using one engine more or less the same as any other, and saves you needing to learn the peculiarities of any specific engine.

  1. First, if you’re working on a UNIX system or your windows build uses Sybase libraries instead of mssql, you’ll need to specify the port number to connect to. SQL Server by default listens on port 1433. If you’re using the windows binary distribution and have enabled the mssql module that ships with it, you don’t need the port number unless a non-standard port number is being used.
  2. The second thing is that you need to be aware of the syntax for using a stored procedure. The syntax is "EXECUTE procedure_name @variable1 = ‘value1′, @variable2 = ‘value2′".


A Basic Example to connect to SQL Server using PHP


$db =& DB::connect("mssql://user:password@host:1433/membership");
if (DB::isError($db)) {

$res = db->query("EXECUTE member_list");
if (DB::isError($res)) {

echo "<table>n<tr>n<th>Name</th><th>Address</th><th>Email</th>" .
while($row =& $res->fetchRow()) {
   $row["name"], $row["address"], $row["email"],
echo "</table>n";


More PHP – SQL Server Connection Examples can be seen here, which describes

  • PHP Code – Connect with a DSN
  • PHP Code – Connect without a DSN (using a connection string)
  • PHP code- How to connect to MS SQL Server database

Information Source

Source code: Microsoft SQL Server 2005 Driver for PHP in CodePlex

Accessing SQL Server Databases with PHP

Download SQL Server 2005 Driver for PHP in the Microsoft Download Center

EOF - PHP Code to access SQL SERVER 2008, SQL Server 2012 , 2008 R2 , 2008 , 2005 , 2000
Leave a comment


  1. Microsoft Drivers for PHP for SQL Server 2.0
    : Microsoft has released of the Microsoft Drivers for PHP for SQL Server 2.0, today (05-August-2010).

    Visit this for code example.

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Notify me of followup comments via e-mail. You can also subscribe without commenting.

Trackbacks and Pingbacks: