PHP Extension for Windows development

Posted on 2013-12-02
Last Modified: 2013-12-08
I am working on a PHP project using VS 2010 and the "PHP Tools for Visual Studio." The VS plugin is working fine and I now need to be able to test my project with a connection to a local SQL database. To do this I have downloaded and installed "Microsoft Drivers for PHP for SQL Server." I have followed the instructions for installing the driver and edited the PHP.ini file as described by the help file to load the extension:


The above DLL is in the "ext" folder for PHP and I double checked the location of the "ext" folder.

When I try to use the slqsrv_connect function in my code it is reported undefined.

The only step I was not able to perform was restarting the web server, however, as I understand it VS starts and stops the web server as debugging is started/stopped.

I hope someone can point me in the right direction to resolve this.

Question by:Sid Price
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39691375
You also need the SQL Native Client for that version of SQL Server for the extension to work.  You might also need the 'nts' version of the dll if you are running IIS.

Author Comment

by:Sid Price
ID: 39691419
I have already downloaded and installed SQLSRV 3.0, that is where I started. I have also tried both the threadsafe and non-threadsafe DLLS. The DLLs mentioned in the link you gave, php_mssql and php_pdo_mssql are not included in that download nor are they mentioned in the PHP.ini file. Is there some other download I need that was not mentioned in the SQLSRV 3.0 download?
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39691436
That comment on that page about 'mssql' is simply wrong.  

The SQL Native Client is not included in the PHP driver download but it is mentioned as a Requirement.  I didn't give you a link because each version of SQL Server has had a different version of SQL Native Client.  Download links on this page:
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.


Author Comment

by:Sid Price
ID: 39691458
OK, so I am using SQL Server 2008 R2, I tracked down the Native Client for it, downloaded and when I tried to install it said a higher version was already installed. So, I guess I already have it.
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39691495
In the Admin panel for Data Sources (ODBC), you will see the versions that are installed in your system.  The other thing to look at is the 'ts' versus the 'nts' DLLs.  I believe IIS requires the 'nts' version and Apache needs the 'ts' version.

Author Comment

by:Sid Price
ID: 39693057
Using the SQL Server Configuration Manager I see that "SQL Native Client 10.0" is installed. I am not sure where to find the Admin panel you mentioned. I am an occasional SQL Server user so not very familiar with all the tools.
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39693270
Administrative Tools is either on the All Programs menu or in the Control Panel.  It is not part of SQL Server, it's part of Windows.  Data Sources (ODBC) is on the menu for Administrative Tools.

Author Comment

by:Sid Price
ID: 39693283
Thanks, SQL Server Native Client 10.0 2009.100.1617.00.
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39693291
Here is my working demo program for PHP with 'sqlsrv' driver.  You will need to put in your own database and table info to run it.  Just upload this to your web server and run it.  I don't know what VS will do to the formatting.
// DB configuration
$dbtable = "yourtable";  // the table we're using
/* Specify the server and connection string attributes. */
$serverName = "YOURSERVER\SQLEXPRESS";    // Your database server
//$serverName = "";    // Your database server
/* Get UID and PWD from application-specific files.  */
$uid = "YOURUID";      // Your db username
$pwd = "YOURPWD";      // Your db password
$connectionInfo = array( "UID"=>$uid,
/* Connect using SQL Server Authentication. */
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn === false )
     echo "Unable to connect.</br>";
     die( print_r( sqlsrv_errors(), true));
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

<title>sqlsrv demo</title>
<table border="0" cellpadding="0" cellspacing="0" summary="">
<tr valign="top">
<h2 align="center" style="margin: 1px;">sqlsrv demo - PHP/MS-SQL</h2>
<div align="center">MS SQL server using PHP 'sqlsrv' driver <?php echo $serverName; ?></div>
<table border="0" cellpadding="0" cellspacing="1" summary="local people" width="960px" bgcolor="#336699" style="font-family: Arial; font-size: 11pt;">
if ($conn != "") { 
// Formulate Query
$pgsize = 20;
/* Assign literal parameter values. */
$params = array( 5, 10);
sqlsrv_query( $conn,"SET ROWCOUNT $pgsize");
// --------- Get data from table ---------------
$dquery = "SELECT TOP ($pgsize) * FROM $dbtable";
$dquery .= " ORDER BY ent_num";
//echo $dquery."<br>";
/* Execute the query. */
if($result = sqlsrv_query( $conn, $dquery, $params, array( "Scrollable" => SQLSRV_CURSOR_KEYSET ))) {
  //echo "Statement executed.\n";
else {
      echo "Error in statement execution.\n";
      die( print_r( sqlsrv_errors(), true));
$nrows = sqlsrv_num_rows($result);

while ($row = sqlsrv_fetch_array($result, SQLSRV_FETCH_NUMERIC)) {
echo '<tr bgcolor="#ffffff">';
echo '<td>&nbsp;'.$row[0].'</td>';
echo '<td>&nbsp;'.$row[1].'</td>';
echo '<td>&nbsp;'.$row[3].'</td>';
echo '<td>&nbsp;'.$row[4].'</td>';
echo '<td>&nbsp;'.$row[5].'</td>';
echo '<td>&nbsp;'.$row[6].'</td>';
echo '</td></tr>';
echo "\r\n";



Open in new window


Accepted Solution

Sid Price earned 0 total points
ID: 39693443
My issue is not that I cannot connect to the database, my issue is that the PHP extension required to access a SQL Server database is not loading.

What I have discovered is that I was loading the wrong extension DLL. My original post showed that I was loading "php_sqlsrv_53_ts.dll", digging into the phpinfo output I saw that PHP 5.4 was running and therefore I needed to use "php_sqlsrv_54_nts.dll". Having loaded the correct DLL I can now connect to my server.

Many thanks for trying to help,

Author Closing Comment

by:Sid Price
ID: 39704084
Because I discovered the solution myself.

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to shrink a transaction log file down to a reasonable size.

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question