Skip to main content

Windows Azure SQL Database PDO

I faced a problem while trying to connect Windows Azure SQL Database from local system using PDO. Here I'm using PHP 5.4+ and Apache 2.4 on Windows 8. I didn't find better tutorial for trouble shooting.

Following are the steps that explains you what to do..

  1. Download php_pdo_sqlsrv_54_ts.dll and placed it in php/ext directory. Here you can find it
    http://www.microsoft.com/en-us/download/details.aspx?id=20098
    Download SQLSRV30.EXE and extracted to php/ext directory
  2. Open the php/php.ini file and added the following line
    extension=php_pdo_sqlsrv_54_ts.dll
  3. It needs Microsoft SQL Server 2012 Native Client, to go further. So download it from
    http://www.microsoft.com/en-us/download/confirmation.aspx?id=29065
    for 32bits(x86)
    http://go.microsoft.com/fwlink/?LinkID=239647&clcid=0x409
    for 64bits(x64)
    http://go.microsoft.com/fwlink/?LinkID=239648&clcid=0x409
  4. Restart the Apache server.
  5. Write the following code in php file to connect.
    $server_url = "xxxxxxx.database.windows.net,1433";
    $db_name = "database_name";
    $db_user = "db_username";
    $db_pwd = "db_pword";
    $conn = new PDO("sqlsrv:Server=tcp:$server_url;Database=$db_name", $db_username, $db_pwd);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
  6. Fetching data from table
    $query = "SELECT * FROM categories";
      $stmt = $conn->prepare($query, array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
      $stmt->excute();
    
      while($cat = $stmt->fetch(PDO::FETCH_ASSOC)){
        echo $cat['cat_id']."---".$cat['cat_name']."<br />";
      }
    
That's it...

Comments

Popular posts from this blog

PHP Code Review Guidelines

General  The code works  The code is easy to understand  Follows coding conventions  Names are simple and if possible short  Names are spelt correctly  Names contain units where applicable  There are no usages of magic numbers  No hard coded constants that could possibly change in the future  All variables are in the smallest scope possible  There is no commented out code  There is no dead code (inaccessible at Runtime)  No code that can be replaced with library functions  Variables are not accidentally used with null values  Variables are immutable where possible  Code is not repeated or duplicated  There is an else block for every if clause even if it is empty  No complex/long boolean expressions  No negatively named boolean variables  No empty blocks of code  Ideal data structures are used  Constructors do not accept null/none values  Catch clause...

PHP script to upload file securely

How to Write a Secure PHP Script for File Uploads File uploads are a common feature in web applications, but they can introduce significant security risks if not handled properly. In this article, we'll walk through the steps to securely upload files to a server using PHP. We'll cover key security measures such as file validation, limiting file types, setting file size limits, and managing file storage. We will also create reusable functions to handle the upload process. 1. Create the HTML Form First, we need an HTML form that allows users to select and upload a file. Ensure that the form uses the POST method and includes the enctype="multipart/form-data" attribute. <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Secure File Upload</title> </head> <body> <form action="upload.php" method="post" enctype="multipart/...

Multiple Checkboxes Validation

This is a regular problem and solution is hardly available on google. When we use multiple checkboxes of same group (same name with square brackets) and you've to read the values from server side, then below solution will help you. Here I'm using jQuery (https://jquery.com/) and jQuery Validate plugin (https://jqueryvalidation.org/) For an example, I've to ask a user which of the listed book they are interested to know about <form id="BooksForm" method="post" name="BooksForm"> <p>Books you are interested in </p> <input class="Books" id="Book1" name="Books[]" type="checkbox" value="1" /> The Inner Game of Tennis <br /> <input class="Books" id="Book2" name="Books[]" type="checkbox" value="1" /> Monk who sold his ferrari <br /> <input class="Books" id="Book3" name=...