• home
  • forum
  • my
  • kt
  • download
  • Building faster Web apps using PHP and in-memory solidDB

    Author: 2009-04-17 09:32:22 From:

    Learn how to use PHP with IBM® solidDB® to build a dynamic Web page application. IBM solidDB is an in-memory database that features extremely fast transaction speeds for queries of even the largest databases. PHP is a scripting language that has become a widely used platform for Web applications. This article shows you how to use PHP to connect to a solidDB database and perform basic operations such as retrieve, update, and delete. You can use these steps as a foundation for building rich Web applications that harness the power of solidDB.

    Introduction

    IBM solidDB is an in-memory database that can perform at speeds up to 10 times faster than conventional databases. The combination of this speed with high availability and failover features makes solidDB a natural choice as the data management component for mission critical applications. And because you can work with solidDB using standard SQL, no special skills are required to harness the potential of this exceptional performance.

    PHP is a scripting language that is widely used as the engine behind dynamic Web page applications. You can use PHP to obtain data from a database, embed that data into HTML markup, and generate dynamic content that displays in a user's browser.

    This article first guides you through installing and configuring the basic components you need for building a dynamic Web page application with PHP and solidDB on a Windows® system. The article then provides descriptions of some basic PHP functions. The last part of the article contains samples that demonstrate how to use PHP to connect to a solidDB database, select data, and embed that data into a Web page. Sample code is also provided that shows you how to update and delete records from the solidDB database.

    Installing and configuring basic application components

    This section describes the steps you can perform to install and configure the basic components for building a dynamic Web page application. Figure 1 shows an architectural overview of the system you can build by following these steps.


    Figure 1. System overview
    Architectural overview of system

    Step 1. Set up a directory structure

    Choose a drive on your computer and create a directory path where you want to install the Apache server. At the end of that directory path, create another sub-directory where you want to install PHP.

    For example, if you choose to install on your E drive, you might create this directory path for Apache:
    E:\Web\WebServer

    ...and this directory path for PHP:
    E:\Web\WebServer\PHP

    Step 2. Download and install Apache

    Download the Win32 Binary (MSI Installer) for the current stable version of the Apache HTTP server. After downloading, run the installation program and install the server in the directory you created for it in Step 1 (for example, E:\Web\WebServer). The Resources section of this article contains a link to the Apache Web site where you can find the download and detailed installation instructions.

    Figure 2 is a screenshot from the Apache installation program.


    Figure 2. Apache HTTP server installation
    Screenshot from Apache installation program

    Step 3. Download and install PHP

    You can choose to install PHP either manually or by using a Windows installer file. Depending on your installation method, go to the PHP download page and get either the zip package file or the Windows installer file for the most recent stable version (for example, php-5.2.8-Win32.zip for manual installation, or php-5.2.8-win32-installer.msi for Windows installer).

    Follow the appropriate installation instructions to Install PHP in the directory you created for it in Step 1 (for example, E:\Web\WebServer\PHP).

    The Resources section of this article contains links to the PHP Web site where you can find the download files and detailed documentation.

    If you installed PHP manually, perform these two additional steps:

    1. Update your Windows Environment Variables so that the Path System Variable includes the directory path where you installed PHP (for example, E:\Web\WebServer\PHP as shown in Figure 3).
    2. In your PHP directory, find the file named php.ini-recommended and rename it to php.ini.

    Figure 3. Updating Path System Variable
    Dialog for updating the                     Path System Variable

    After completing either type of installation (manual or Windows installer), you need to change the following settings in your php.ini file:

    1. Find the doc_root setting and set it to point to the htdocs directory of your Apache server (for example, doc_root = E:\Web\WebServer\Apache2\htdocs)
    2. Find the extension_dir setting and set it to point to your PHP ext directory (for example, extension_dir = E:\Web\WebServer\PHP\ext)

    Step 4. Install solidDB

    If you do not already have solidDB installed, purchase it through your normal channel for obtaining IBM software and follow the installation instructions.


    Figure 4. solidDB installation screen
    solidDB installation screen

    Step 5. Add solidDB as an ODBC data source

    ODBC is an API that is used to connect to a data source. Follow these steps to add solidDB as a ODBC data source on your Windows system:

    1. From your Windows Control Panel, select Administrative Tools.
    2. From the Administrative Tools dialog, select Data Sources (ODBC).
    3. From the ODBC Data Source Administrator dialog, click on the System DSN tab. Figure 5 shows what you should now see.

    Figure 5. ODBC Data Source Administrator dialog
    ODBC Data Source Administrator dialog
    1. Click Add.
    2. From the Create New Data Source dialog, find and select the solidDB driver, and click on Finish.
    3. From the next dialog, click on Select to locate the database.
    4. Provide a Data Source Name and Description as shown in Figure 6, and click OK.

    Figure 6. SOLID ODBC Driver Setup dialog
    SOLID ODBC Driver Setup dialog


    Back to top


    Using basic PHP functions

    This section describes and shows examples of some common PHP functions that you will find useful when developing dynamic Web page applications.

    Connecting to an ODBC: odbc_connect()

    Use the PHP odbc_connect() function to connect to an ODBC data source. The function has four parameters:

    • DSN name
    • Username
    • Password
    • Cursor type (optional)
    In the following example the first line sets the variable $conn as a connection to the solidDB database. The next line defines an SQL query, and the final line executes the query against the solidDB database and puts the results in a variable named $rs.

    			
    $conn=odbc_connect('solidDB','dba','dba');
    $sql="SELECT * FROM addressbook"; 
    $rs=odbc_exec($conn,$sql);
                

    Retrieving records: odbc_fetch_row()

    After a query is executed, use the odbc_fetch_row() function to retrieve records from a result set. The only parameter for this function is the result set identifier.

    The following example builds on the code from the previous section. It fetches the first record from the $rs result set:

    odbc_fetch_row($rs)
                

    Reading field values: odbc_result()

    Use the odbc_result() function to read field values from a retrieved record. The function takes two parameters:

    • ODBC result identifier
    • Field number or name

    The following examples builds on the code from the previous sections. The first example uses a field number to sets the variable $firstname to the value of the first field of the $rc record. The second example uses a field name to set the same variable to the field in the $rc record named Firstname.

    $firstname=odbc_result($rs,1);	
    		
    

    $firstname=odbc_result($rs,"Firstname");
      

    Closing an ODBC connection: odbc_close()

    Use the odbc_close() function to close an ODBC connection. The only parameter for this function is the connection identifier.

    The following example builds on the code from the previous sections. It closes the $conn connection.

    odbc_close($conn);
    		



    Back to top


    Sample code listings

    This section contains listings of sample PHP code. Samples are also provided as part of the zip file in the Download section of this article.


    Listing 1. Select and display all records
    <html>
    <body>
    <?php 
    
    $counter = 0;
    //connection - connect to the database
    $connection = odbc_connect('solidDB','dba','dba');
    
    
    $query = "select * from addressbook";
    
    //execute the query and get result
    $result = odbc_exec($connection, $query);
    print("<table border=4>");
    
    print("<tr bgcolor='blue'><td>ID</td><td>NAME</td><td>SURNAME</td><td>GENDER</td>
    <td>ADDRESS</td><td>DOB</td><td>EMAIL</td><td>PHONE</td><td>DESCRIPTION</td>");
    
    
    while(odbc_fetch_row($result)) {
    $counter+=1;
    $id = odbc_result($result, 1); //unique identifier
    $fname = odbc_result($result, 2); //first name
    $lname = odbc_result($result, 3); //last name "surname"
    $gender = odbc_result($result, 4); //gender
    $address = odbc_result($result, 5); //address field
    $dob = odbc_result($result, 6); //date of birth
    $email = odbc_result($result, 7); //email id
    $phone = odbc_result($result, 8); //phone number
    $descr = odbc_result($result, 9); //comment or description
    
    print("<tr><td>$id</td><td>$fname</td><td>$lname</td><td>$gender</td>
    <td>$address</td><td>$dob</td>
    <td>$email</td><td>$phone</td><td>$descr</td>");
    
    }
    print("</table>");
    print("<br>");
    print("<p>$counter records found"); 
    odbc_close($connection); //close the connection
    ?>
    </body>
    </html>
    
    		


    Listing 2. Update records
    <html>
    <body>
    <?php 
    
    //connection - connect to the database using the default dba login
    $connection = odbc_connect('solidDB','dba','dba') ;
    $fname = $_GET['fname']; //get first name
    $lname = $_GET['lname']; //get last name
    $gender = $_GET['gender']; //get gender
    $address = $_GET['address'];//address field
    $phone = $_GET['phone']; //phone number
    $dob = $_GET['dob']; //date of birth
    $descr = $_GET['descr']; //any description
    $email = $_GET['email']; //email id
    $id = $_GET['id']; //unique identifier
    //update query
    $query="update inform set fname='$fname',lname='$lname',gender='$gender',";
    $query.="phone='$phone',email='$email',birthdate='$dob',description='$descr'where id=$id";
    $query.=",address='$address'";
    odbc_exec($connection,$query); //execute the query
    odbc_close($connection); //close the connection
    ?>
    
    </body>
    </html>
    
    	


    Listing 3. Delete a record
    <html>
    <body>
    <?php 
    
    //connection - connect to the database 
    $connection = odbc_connect('solidDB','dba','dba') ;
    //
    $id = $_GET['id'];
    // delete query
    $query = "delete from inform where id=$id";
    //execute the query 
    odbc_exec($connection, $query);
    print "<h2>RECORD DELETED</h2>";
    
    odbc_close($connection);
    ?>
    </body>
    </html>
     



    Back to top


    Conclusion

    This article introduced IBM solidDB and the PHP scripting language. It provided an overview of the steps for installing and configuring basic components that you can use to develop a dynamic Web page application, and demonstrated how to perform basic database operations with PHP. Once you have masted how to use these basic components and methods, you will have the tools needed to build rich and powerful Web applications that are able to perform at exceptional speeds even under peak workloads.

    discuss this topic to forum

    relation tutorial

    No information

    Category

      Ad Management (6)
      Calendars (3)
      Chat Systems (8)
      Content Management (45)
      Cookies and Sessions (12)
      Counters (15)
      Database Related (34)
      Date and Time (15)
      Development (22)
      Discussion Boards (8)
      E Commerce (8)
      Email Systems (14)
      Error Handling (8)
      File Manipulation (36)
      Flash and PHP (6)
      Form Processing (22)
      Guestbooks (12)
      Image Manipulation (26)
      Installing PHP (7)
      Introduction to PHP (29)
      Link Indexing (8)
      Mailing List Management (9)
      Miscellaneous (60)
      Networking (9)
      News Publishing (9)
      OOP (28)
      PEAR (6)
      PHP vs Other Languages (2)
      Polls and Voting (7)
      Postcards (1)
      Randomizing (15)
      Redirection (12)
      Searching (10)
      Security (32)
      Site Navigation (16)
      User Authentication (16)
      WAP and WML (7)
      Web Fetching (10)
      Web Traffic Analysis (15)
      XML and PHP (18)

    New

    Hot