An Introduction to PHP3
Listing 1 is two examples of PHP code to show some of its features. The first page accepts two numbers to add a first and last name. The passing of the form variables to be used in the next page happens automatically, which is one of the neatest things about PHP. This doesn't check their type—that they are actually numbers—in order to give an error. It does show the way PHP can be used to send and receive information to the user.
Listing 2 is an example that generates CGI variables on the fly, and the receiving page (Listing 3) deals with any incoming variables generated. If you run this in a PHP-enabled server, you'll see the variables passed in the cgi-call in the location line of the browser. All Listing 3 does is echo the names of the variables, but it demonstrates a technique that would enable a web site to generate a list on the fly and allow the user to make selections—like a shopping cart.
Now let's connect to a MySQL database. The following code would connect as root to a database named “stores” on the local host, with password “tiger”. It executes a query against a parts table, counts the number of rows in the query to check for the actual data, and if it exists, the result is displayed as an HTML table with check boxes to select items. Listing 4 is the PHP code preceded by the structure of the table. Note that knowing the number of rows is handy for determining whether to display the query at all (if there is any information) as well as counting down until no data is left. Each row of data is a row in an HTML table, with the fields walked through by number. The array containing the row from the query can be addressed by number or by field name, so you could also execute:
This can be advantageous during development when field positions in the table or query may be changing more often than names.
The next page of HTML (called in the FORM command) will receive the checked-off selections as CGI variables, which can be retrieved even though their names are unknown, using the technique shown above. It will then insert into another table the items that have been checked off. See Listing 5.
The latest information on installing PHP is available at http://www.php.net/ or in the distribution itself. I have installed it for FreeBSD, Linux and Win32 using their supplied packages. I have installed it only for Apache, although it is supposed to work with other web servers. It is supplied as source code for platforms other than Win32. The installation for Apache requires recompiling Apache to add it in as a module, although if that is not an option, it can be installed as a binary and scripts can be run as cgi-bin scripts.
I've found PHP3 to be a very enjoyable and versatile language for web applications. As of this writing, the next version—which is primarily billed as a performance improvement—is in beta testing. (PHP 4.0 includes the ZEND engine; see their web site for details.)
John Holland programs intranet applications as a consultant for Bell Atlantic. He lives outside Washington DC with his wife and children. He can be reached at firstname.lastname@example.org.
Webinar: 8 Signs You’re Beyond Cron
11am CDT, April 29th
Join Linux Journal and Pat Cameron, Director of Automation Technology at HelpSystems, as they discuss the eight primary advantages of moving beyond cron job scheduling. In this webinar, you’ll learn about integrating cron with an enterprise scheduler.Join us!
|Play for Me, Jarvis||Apr 16, 2015|
|Drupageddon: SQL Injection, Database Abstraction and Hundreds of Thousands of Web Sites||Apr 15, 2015|
|Non-Linux FOSS: .NET?||Apr 13, 2015|
|Designing Foils with XFLR5||Apr 08, 2015|
|diff -u: What's New in Kernel Development||Apr 07, 2015|
- Drupageddon: SQL Injection, Database Abstraction and Hundreds of Thousands of Web Sites
- Play for Me, Jarvis
- Non-Linux FOSS: .NET?
- Designing Foils with XFLR5
- Not So Dynamic Updates
- Flexible Access Control with Squid Proxy
- New Products
- Users, Permissions and Multitenant Sites
- diff -u: What's New in Kernel Development