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
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
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:
- 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).
- In your PHP directory, find the file named php.ini-recommended and rename it to php.ini.
Figure 3. Updating 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:
- 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)
- Find the extension_dir setting and set it to point to your PHP ext directory (for
example,
extension_dir = E:\Web\WebServer\PHP\ext)
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
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:
- From your Windows Control Panel, select Administrative Tools.
- From the Administrative Tools dialog, select Data Sources (ODBC).
- 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
- Click Add.
- From the Create New Data Source dialog, find and select the solidDB driver, and click on Finish.
- From the next dialog, click on Select to locate the database.
- Provide a Data Source Name and Description as shown in Figure 6, and click OK.
Figure 6. SOLID ODBC Driver Setup dialog
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); |
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>
|
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.
| Description | Name | Size | Download method |
|---|---|---|---|
| Example PHP scripts for this article | examplecode.zip | 12KB | HTTP |
Information about download methods
Learn
-
Apache HTTP Server
project home page:
Find links to documentation, mailing lists, and other Apache
resources.
-
PHP home page: Find links to instructions
for installing and configuring PHP, mailing lists, and other PHP
resources.
-
IBM solidDB Home
Page:
Learn more about IBM solidDB.
-
W3Schools PHP Database
ODBC:
Learn more about PHP and ODBC.
-
developerWorks Information Management zone:
Learn more about DB2. Find technical documentation, how-to articles,
education, downloads, product information, and more.
- Stay current with
developerWorks
technical events and webcasts.
Get products and technologies
- Download the
Apache HTTP Server
installation program.
- Download the
PHP installation
program.
Discuss
- Participate in the discussion forum.
- Check out
developerWorks blogs
and get involved in the
developerWorks community.

Abhinay Nagpal is a DB2 advanced technical support analyst at IBM India Software Lab, Pune, where his main focus is assisting IBM DB2 customers worldwide.






