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

Survey says: PHP passes Microsoft Active Server Pages

By JT Smith on June 11, 2002 (8:00:00 AM) With a faltering economy forcing companies to cut spending whenever possible, less expensive and freely available Open Source software solutions may be gaining in popularity. Those wanting proof can look no further to PHP taking the top server-side scripting spot in a recent Internet host survey. In April 2002, Netcraft's monthly Web server survey revealed that 24 percent, or around 9 million of the 37 million sites it surveyed, were using Hypertext Preprocessor (PHP) for a server side scripting language. For the first time, an Open Source scripting solution had passed Microsoft's proprietary Active Server Pages scripting to claim the top spot on the Netcraft survey. For both the April and May Netcraft surveys, PHP and ASP were almost too close to call, with Microsoft's product offering coming in just a hair under 24 percent of all hosts running a server-side script...

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

The 7 Types of Leadership: Inspiring Examples and Insights

Leadership is a multifaceted concept with various styles that cater to different needs and situations. Understanding these styles can help you develop your leadership skills and adapt to diverse scenarios. In this article, we'll explore seven prominent leadership styles, each accompanied by insightful examples from influential authors. Let's dive in! Type 1: Transformational Leadership 🚀 Transformational Leadership: Inspiring Change and Innovation Transformational leadership focuses on inspiring and motivating followers to achieve extraordinary outcomes and, in the process, develop their own leadership capacity. Example from Author: James MacGregor Burns James MacGregor Burns, in his book "Leadership," describes transformational leaders as those who seek to change the status quo by appealing to their followers' values and sense of higher purpose. 🔸 Characteristics: - Inspirational Motivation - Intellectual Stimulation - Individualized Consideration - Idealized I...