You are now ready to check out your PHP install as well as start writing PHP code that works with SQL Server. First off, create a simple text file in Notepad with the following code and save it as test.php as your Web server's web root folder. For instance in c:\inetpub\wwwroot\ php\test.php.
phpinfo(); ?>
Open Internet Explorer and point to http://localhost/ php/test.php. You should now see the PHP status information page. Check out whether the extension named sqlsrv is loaded. Now comes the time to actually connect to the database. For this, there is one more optional step that you may need to perform.
In case you are running SQL Server 2005 on the same machine as where the PHP code is running, you need not do anything, but write your code. However, in case you are running SQL Server 2005 on a remote machine or SQL Server 2008 on the same or a remote machine, you will need to install the SQL Server 2005 Native Client on the Web server.
This is because although the MS driver for PHP is optimized to work with SQL Server it works only with the SQL Server 2005 native client. You can download this from the Microsoft site or get it off the DVD. After this, you can start writing code to connect to SQL Server. Take a look at the code below for a small sample.
"AdventureWorks2008", "UID"=>"sa", "PWD"=>"password");$conn = sqlsrv_connect("(local)", $connectionInfo);if( $conn === false ){ echo "Could not connect."; die( print_r( sqlsrv_errors(), true));}$tsql = "SELECT ProductID, UnitPrice, StockedQty FROM Purchasing.PurchaseOrderDetail WHERE StockedQty < 3 AND DueDate='2002-01-29'";echo "Connecting to AdventureWorks2008 on local SQL Server 2008";$stmt = sqlsrv_query( $conn, $tsql);if ( $stmt ) echo "Statement executed.";else { echo "Error in statement execution."; die( print_r( sqlsrv_errors(), true));}while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_NUMERIC)){ echo "ProdID: ".$row[0].""; echo "UnitPrice: ".$row[1].""; echo "StockedQty: ".$row[2]."";}sqlsrv_free_stmt( $stmt);sqlsrv_close( $conn);?>
Save this file as say get.php in the same location and browse over to http://localhost/php/get.php. You should see a few records from the AdventureWorks database being shown. The SQL Server driver for PHP is a great way of connecting to SQL Server databases from PHP. Written by Microsoft themselves, this offers much better performance and stability than stock or 3rd party drivers.
As mentioned, if you have the option of using PHP on IIS7 and SQL Server for a large and scalable application, it makes a good choice to do so and these drivers let you work very nicely with the database. Do note that these drivers are open source and Microsoft has supplied the source at CodePlex where you can download, view and change them if you wish.
Get most out of your technology infrastructure investments with Dell
About CIOL | Media Kit | Site Map | Contact Us | Help | Write to us | Jobs@CyberMedia | Privacy Policy
Copyright © CyberMedia India Online Ltd. All rights reserved. Usage of content from web site is subject to Terms and Conditions.