PHP Extension for Windows development

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:

extension=php_sqlsrv_53_ts.dll

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.

Thanks,
Sid.
Sid PriceSoftware Systems Architect/DesignerAsked:
Who is Participating?
 
Sid PriceConnect With a Mentor Software Systems Architect/DesignerAuthor Commented:
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,
Sid.
0
 
Dave BaldwinFixer of ProblemsCommented:
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.  http://us3.php.net/manual/en/sqlsrv.installation.php
0
 
Sid PriceSoftware Systems Architect/DesignerAuthor Commented:
Dave,
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?
Thanks,
Sid.
0
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.

 
Dave BaldwinFixer of ProblemsCommented:
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: http://msdn.microsoft.com/en-us/library/cc296170.aspx
0
 
Sid PriceSoftware Systems Architect/DesignerAuthor Commented:
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.
Sid.
0
 
Dave BaldwinFixer of ProblemsCommented:
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.
0
 
Sid PriceSoftware Systems Architect/DesignerAuthor Commented:
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.
Sid.
0
 
Dave BaldwinFixer of ProblemsCommented:
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.
0
 
Sid PriceSoftware Systems Architect/DesignerAuthor Commented:
Thanks, SQL Server Native Client 10.0 2009.100.1617.00.
Sid
0
 
Dave BaldwinFixer of ProblemsCommented:
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.
<?php 
// DB configuration
$dbtable = "yourtable";  // the table we're using
/* Specify the server and connection string attributes. */
$serverName = "YOURSERVER\SQLEXPRESS";    // Your database server
//$serverName = "10.202.46.40";    // 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,
                         "PWD"=>$pwd,
                         "Database"=>"TestDB");
/* 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"
 "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>sqlsrv demo</title>
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0" summary="">
<tr valign="top">
<td>
<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;">
<tbody>
<?php
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";
// TEST PRINT!!
//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";
}
}
?>
</tbody> 
</table>

</td>
</tr>
</table>

</body>
</html>

Open in new window

0
 
Sid PriceSoftware Systems Architect/DesignerAuthor Commented:
Because I discovered the solution myself.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.