MSSQL Driver for PHP5.3

Before using PHP5.3, I never had to worry about MSSQL support. PHP included a dll file to use with MSSQL Server 2000 and up. This is no longer true if you decide to upgrade to PHP5.3.x. For months I looked for ways to connect to our MSSQL Server using PHP5.3.x. I finally found the driver and it works. It’s called SQLSRV and you can download it from http://sqlsrvphp.codeplex.com. It’s easy to install, just read the CHM file.

  • Run the exe to unpack it
  • Rename the folder to Microsoft SQL Server Driver for PHP (optional)
  • Move this to your %Program Files% directory (optional)
  • Inside that directory you will find several dll files. It supports both NTS (Non Thread Safe) and TS (Thread Safe). You also need to choose between VC6 or VC9 compiler. This will determine which file will be used.
  • Copy that file to %Installation Directory%PHPext (assuming you are using the ext directory for your extensions.
  • Edit your php.ini. In the extensions section add the following extension=php_sqlsrv_53_ts_vc6.dll – or whatever file you copied to your ext directory.
  • Restart your web server and that should be it.

Here’s a quick sample code to get you started.

<?php

$servername = "SQLSERVER_NAME_OR_IP";
$uid = "db_username";
$pwd = "db_passwd";
$connectionInfo = array (
"UID"=>$uid,
"PWD"=>$pwd,
"Database"=>"database_name"
);
$conn = sqlsrv_connect($serverName, $connectionInfo);

if ($conn === false) {
echo "ERROR: DB Connection";
die(print_r(sqlsrv_errors(), true));

}
$stmt = sqlsrv_query($conn, "SELECT * FROM table");

if ($stmt) {
while ($row = sqlsrv_fetch_array($stmt)) {
echo $row[0].'<br />';
}
}

?>

That should do it. I have only tested this on Windows 7 Professional (32bit), MSSQL 2005 (32bit) Standard on a remote Windows 2008 Server Standard R2, PHP5.3.3, Apache2.2.15, and driver version 1.1 (version 2 is now available) . Make sure your MSSQL server can accept remote connections if it is on a different server than your web server. The above instructions shouldn’t be too different on other versions.

If you have a phpinfo() page, you will see the image below.

sqlsrv

Hope this helps you get started. Happy coding.

7 thoughts on “MSSQL Driver for PHP5.3

    • if you check out the link i posted…

      The SQLSRV extension provides a procedural interface while the PDO_SQLSRV extension implements PDO for accessing data in all editions of SQL Server 2005 and later (including SQL Azure).

      based on that, i don’t think it’s compatible.

      Like

  1. Hi I am working with a SQL database and want to begin using PHP scripts on the data.I have been trying for hours to connect and not getting anywhere… I sucessfully installed SQLSRV and see it in phpinfo(), but I keep getting this error:Array ( [0] => Array ( [0] => IMSSP [SQLSTATE] => IMSSP [1] => -49 [code] => -49 [2] => This extension requires the Microsoft SQL Server 2011 Native ClientThe SQL server is on a remote machine, and I usually connect with management studio. I am created a DSN but I am not sure how to use it with SQLSRV.

    Like

    • @andrew based on the error message, are you using MSSQ Server 2011 or 2012? according to the documentation where you download the driver, it’s compatible with all versions of 2005. it doesn’t mention any other version. i could be wrong. another to check is the $servername. does it have the correct SQL Server name or IP, if so make sure it allows for remote connections. if you have been connecting with management studio from a remote computer then that may not be the problem. i’m guessing that your SQL server version is not compatible. i’m for certain. i only have access to MSSQL 2005.

      Like

      • The server I am trying to connect to is 2005, in all the documentation there is mention that the SQL Server must be installed locally on the machine. I also downloaded the latest SQLSRV 3.0I may have a newer version of SQL Server installed locally when I installed Visual Studio.Is your server local or remote? Syntax for the ip is $serverName = “000.000.000.000”;I am using SQL authentication if that makes any different..Really not sure what variable are relevant here in troubleshooting.

        Like

      • yes, the SQL server is different from the web server. it’s a windows 7 desktop. i connect using the IP for the value of $servername. i only tried this to play around with the idea just in case my work asks for such a setup. i’ll revisit the setup once i get in at work and see what else i find.

        Like

      • IT FINALLY WORKED!!!! I installed 2012 SQL native client on my local machine running the PHP and eureka!I used to get excited about beating a level in a video game, lifting more weight in the gym, or even getting laid by a girl that I really liked.Now a days this sort of triumph is all I need to excite me for the day!Thanks so much for this informative post!

        Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s