Solved

PHP Extension for Windows development

Posted on 2013-12-02
11
289 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 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.  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 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: 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 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.
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

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

Author Comment

by:Sid Price
ID: 39693283
Thanks, SQL Server Native Client 10.0 2009.100.1617.00.
Sid
0
 
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.
<?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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

932 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

10 Experts available now in Live!

Get 1:1 Help Now