Solved

PHP Extension for Windows development

Posted on 2013-12-02
11
281 Views
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:

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.
0
Comment
Question by:Sid Price
  • 6
  • 5
11 Comments
 
LVL 82

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.  http://us3.php.net/manual/en/sqlsrv.installation.php
0
 

Author Comment

by:Sid Price
ID: 39691419
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
 
LVL 82

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: http://msdn.microsoft.com/en-us/library/cc296170.aspx
0
 

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.
Sid.
0
 
LVL 82

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.
0
Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

 

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.
Sid.
0
 
LVL 82

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.
0
 

Author Comment

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

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.
<?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
 

Accepted Solution

by:
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,
Sid.
0
 

Author Closing Comment

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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

757 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now