|
November 2001 Boosting your e3000
productivity Try a
new language: PHP One of PHPs
strengths is easy database integration. Another is that it runs on
many platforms, including Windows, LINUX and HP-UX, and even somewhat
on MPE/iX: a Web page at invent3k.external.hp.com/~MGR.FETHERS
describes an in-progress port by Campbell Fethers and Troy Jendra,
including a TurboIMAGE database interface. The Daily News Page (www.news.ai/daily.php3) is updated whenever there is some news about the Caribbean island of Anguilla. Some days there is no news, but many site visitors refresh the page during the day to see if there is anything new. Problem: How to keep these frequent visitors happy? Solution: generate dynamic content from the archives that varies on every refresh. This Web page selects a random picture from a list of over 1,000 pictures, plus a link to a Web page that explains the picture. After error checking the picture filename (Does it exist? Is it readable? Is it too small or too big?), the PHP script displays the picture resized to a standard size, with a link to zoom to the full-size image. Then it adds the date of the picture and a link to the accompanying story. Here is an edited extract from the daily.php3 script to show you some PHP coding: <?php $pixdb = file('/home/anguillanews/pixdb.txt'); $count=count($pixdb); // seed and pick a random number between 0 and $i: srand((double)microtime()*1000000); $index = rand()%$count; $pixentry = explode(",",trim($pixdb[$index])); $filename = trim($pixentry[0]); $link =trim($pixentry[1]); /*story about this pix */ if ( (!file_exists($link)) || (!is_readable($link)) ) { $link = ""; } if ( (!file_exists($filename)) || (!is_readable($filename)) ) { $filename = ""; } if ($filename == "") { error_log("News/bad line in pixdb: $index", 1,"bgreen@robelle.com",""); ... ?>This code is easy to follow: variables start with $ and values can be passed in via the URL if desired. Statements end with a semicolon, statement blocks are enclosed in braces, comments are enclosed in /* and */, and there are many built-in functions, like rand and file and count. The script opens a file containing a list of paired images and Web pages, computes a random location in that file, reads the location, extracts the image file name and the link URL. Then it does some error checking on the data, and if it doesnt find a valid pair, it logs an error, which ends up being sent to me as an e-mail. Finally the script continues to generate the HTML to display the picture and the link (this is not shown above) If you are new to PHP and want to get some idea of how it works, try the introductory tutorial at the PHP home page (www.php.net/tut.php). You can also check out the online manual (www.php.net/docs.php). I found the Annotated User Manual (www.php.net/manual) very helpful. It is a Web-based document that allows you to append questions and clarifications to each page. The combined knowledge of the PHP community helps clear up a lot of special cases that can be confusing. If you want to experiment
with PHP, you can install it on most Windows or UNIX systems, or open
an account at a Web hosting service like www.pair.com. Another place where PHP is commonly used is in database access: retrieval, display, and update. PHP has convenient built-in functions for Oracle, MS SQL Server, ODBC, dBase, mySQL, PostgreSQL, FrontBase, filePro, Informix, and InterBase. Troy Jendra has created a rough TurboIMAGE interface for PHP. We wanted to make an archive of our customer records, and selected mySQL because it was free, very fast for retrievals, reliable, and good enough for a read-only archive. To search and display the data we used the Xitami Web server and PHP scripts (we found many good sample scripts on the Internet to get us started). The whole project was done on a Windows PC. To read about our conversion from TurboIMAGE to mySQL, visit www.robelle.com/tips/mysql.html. Below is the search Web page form; it prompts for an account number and a company name and passes them to the search.php script: <b>Search the Database:</b> <p> <FORM METHOD="post" ACTION="search.php"> Account Number: <INPUT TYPE=text MAXLENGTH=4 NAME="account_no" SIZE=20><Br> Company Name: <INPUT TYPE=text MAXLENGTH=60 NAME="company_name" SIZE=20><br> <INPUT TYPE=submit VALUE="Search"> </FORM> And here is part of the search.php script, showing how it takes the $account_no and $company_name fields from the Web search form (all form fields automatically become variables in the PHP script) and turns them into a database query: <? mysql_connect("localhost", "username", "passwd") or die ("Unable to connect to server."); mysql_select_db("kco") or die ("Unable to select database."); $andor = 'and'; if(empty($account_no)) { $andor = 'or'; $account_no = '%'; if(empty($company_name)) { print"No Search Keywords Specified"; exit; } } else { if(empty($company_name)) { $company_name = '%'; } } $query = "SELECT * FROM m_customer where account_no='$account_no' $andor company_name like '%$company_name%'"; $result = mysql_query ($query) or die ("Query Failed"); $num_of_rows = mysql_num_rows ($result);
PHP/iX hasnt progressed significantly since I posted the page on invent3k, due to lack of time, etc. The changeover of invent3k to GCC 3 broke the build of the MPE module. As mentioned on the Web page, we successfully converted one typical complex Speedware report to a PHP/iX script, thus proving the concept. In the meantime, we are running PHP on a Linux box with Apache. Troy compiled the Java module into PHP, and wrote some Java objects to interface between PHP and JDBC access to ImageSQL. Response times have been pleasing. This has enabled Web development to proceed, and this system is already being used for production. Future conversion of these Web pages to PHP/iX, while it wont be a simple search and replace, is still expected to be reasonably straightforward. A CGI executable of PHP/iX is available for download from invent3k.external.hp.com/~MGR.FETHERS/download.html. I still intend to finish PHP/iX because I think that IMAGE and PHP are such a neat fit. The only question is when? Here are some resources to help in learning about PHP: Core PHP Programming by Leon Atkinson; Professional PHP Programming from Wrox (ISBN 1861002963 www.php.net - this is the main PHP home page. You can download the PHP server software from here and find the online documentation (annotated!) www.zend.org - Zend is the underlying
engine for PHP 4 and Zend sells add-on products to make PHP better.
Excellent site with vast amount of information, articles, code,
samples. At phpbuilder.com there are PHP script examples, and
www.alt-php-faq.org has Frequently Asked Questions. www.webmasterbase.com/printTemplate.php?aid=228
shows how to build an application with PHP and mySQL. At
hotscripts.com/PHP theres lots of PHP resources, including
sample scripts. Copyright The 3000 NewsWire. All rights reserved. |