Jaa


Host Wordpress on Windows Azure: Consume SQL Azure Using PHP

our wordpress live sample website:

https://wordpress-win-azure.cloudapp.net/

 

 Download sample project here

 

This tutorial article will show you how to consume SQL Azure using PHP with just several easy steps.  Please refer first series article Run PHP application in Windows Azure for a prerequisite.

 

1. To enable PHP to access SQL Azure, SQL Server Driver must be added to PHP extensions. (For SQL Azure is built on SQL Server).Download it form SQL Server Driver for PHP 2.0.

2.Open and extract it, driver files are listed below.

 

We choose php_sqlsrv_53_nts_vc9.dll, for it matches our PHP version, VC9 x86 Non Thread Safe.

Put the driver into php\ext folder in the PHP _WebCgiRole project.

Rename php.ini-develop in the php folderto php-ini. Right click on php.ini, choose Properties, and change the value of Build Action to Content.

Click to Open php.ini, look up extension_dir and change its value to "./ext" . It indicates the extension directory of PHP.

 

Add "extension = php_sqlsrv_53_nts_vc9.dll "to php.ini.

 

4. Put the PHP code below to index.php (refer to Run PHP application in Windows Azure ) .Please refer to here  to replace the SQL Azure account information with yours.

 

 

<?php

$serverName = "tcp:ServerID.database.windows.net, 1433";

$connectionOptions = array("Database" => "yourdatabase",

"UID" => "yourusername@ServerID",

"PWD" => "yourpassword",

);

$retry = 10;

$conn ;

while($retry--)

{

  $conn = sqlsrv_connect($serverName, $connectionOptions);

  if($conn)

  {

    echo "Connect Success!";

    break;

  }

}

if($conn === false)

{

   die(print_r(sqlsrv_errors(), true));

}

?>

 

 

 

 

 

 

 

Verification:

5. Press F5 to debug the application, wait for the browser to start, you'll see the result below:

 

 

 

 

 

You can put it into Windows Azure Hosted service now, and will get the same result.

The process is still simple and easy as before, now you can exploit the power of SQL Azure!

 

References:

https://msdn.microsoft.com/en-us/library/ff394110.aspx

https://blogs.msdn.com/b/brian_swan/archive/2010/02/12/getting-started-with-php-and-sql-azure.aspx

https://channel9.msdn.com/learn/courses/Azure/SQLAzure/IntroToSQLAzure/Exercise-1-Preparing-Your-SQL-Azure-Account/

 

 

 

 

 

 

 

Topic

Description

Chapter 1: Using PHP application in Windows Azure

This article will show how easy to run PHP application inWindows Azure

Chapter 2: Consume SQL Azure Using PHP

This article describes how to access SQL Azure using PHP.

Chapter 3: Host Wordpress on Windows Azure

This article describes the process about host Wordpress on Windows Azure. Similarly, you will see how easy it will be.

Chapter 4: Using Windows Azure Storage in Wordpress

This article introduces the Windows Azure Storage plugin for WordPress . It enables Wordpress store multimedia contenton Windows Azure Storage.

Comments

  • Anonymous
    September 07, 2010
    Hi, your link for SQL Server Driver for PHP seems broken. You may want to change to this updated link: www.microsoft.com/.../details.aspx

  • Anonymous
    September 08, 2010
    Hi Wely, Thanks for your input. we have updated the link.

  • Anonymous
    December 19, 2010
    Hello, I  follow your tutorial, and pass through Chapter 1. But I stuck at Chapter 2. I got 3 error popups when I hit F5. "php-cgi.exe - Entry Point Not Found x The procedure entry point php_set_error_handling could not be located in the dynamic link library php5.dll." "php-cgi.exe - System Error x The program can't start because php_pdo.dll is missing from your computer. Try reinstalling the program to fix this problem." "php-cgi.exe - System Error x The program can't start because php_mbstring.dll is missing from your computer. Try reinstalling the program to fix this problem." I carefully recheck again, and try to remove some code in index.php. I found that if I remove "sqlsrv_connect($serverName, $connectionOptions);" the problem was gone. Do you have any idea? Thank

  • Anonymous
    December 19, 2010
    I'm sure that SQL Azure account information is correct because I can connect to my database using Microsoft SQL Managment Studio. and I have new infos. when index.php don't have "$conn = sqlsrv_connect($serverName, $connectionOptions);" then I got blank page. no error. then if insert "$conn = sqlsrv_connect($serverName, $connectionOptions);" then I got the 3 error popups. and "Http 500 Internal Server Error" then if I change "$conn = sqlsrv_connect($serverName, $connectionOptions);"  to " sqlsrv_connect($serverName, $connectionOptions);"   then I got "Http 500 Internal Server Error", no the error popups. PS. I use SDK 1.3 and I need to comment <Sites> section in ServiceDefinition.csdef because of a error which discuss here social.msdn.microsoft.com/.../a000c4ec-8d92-4320-8328-8521323c60cd I still stuck in Chapter 2.   T T

  • Anonymous
    December 19, 2010
    Update: I add 2 more extensions in "php-ini" file    extension=php_pdo_sqlsrv_53_nts_vc9.dll    extension=php_mbstring.dll and the 3 error popups has gone. BTW I still got "Http 500 Internal Server Error" page.

  • Anonymous
    December 19, 2010
    update: I also try the sample project, and It's doesn't work.