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:

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.


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

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

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

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

627 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