|
|
|
|
|
|
|
|
More PHP Basics, Part 2
P.G. Daly 3/26/2007 The development platform PHP continues to grow in popularity, becoming more of a powerhouse in 2007. Two new recent releases of PHP drive home the point that there is constant care and feeding of this platform by the open source community as usage continues to grow. In February, version 5.2.1 was released to offer enhanced security and bug fixes to the version 5 series. Most recently in March, version 4.4.6 was released to solve a crash problem from the previous version in the version 4 series release. For the non-programmer, PHP (Hypertext Preprocessor) is an open source, server-side, HTML embedded scripting language used to create dynamic Web pages. PHP script is embedded within an HTML document, and the programmer can jump between HTML and PHP seamlessly within the document while the code remains inaccessible to users because it is processed on the server. It combines the power of CGI scripting with the ease of working within HTML. Here's part two of a continuing series on PHP Basics. Interacting with mySQL The most powerful part of any dynamic programming language is its ability to interact with a database. PHP works almost effortlessly with the mySQL database (define). For this example, let's assume you already have created a table called "Employees" with the following fields: FirstNameAnd have it populated with the following data:
The URL where your database is hosted is my.company.com, the Database Name is "CompanyData," the username is "admin," and the password is "getmein."
The first thing you need to do in PHP is establish a connection to the database. You can do that with this code:
<?php
// Connects to the Employee Database
mysql_connect("my.company.com", "admin", "getmein") or die(mysql_error());
mysql_select_db("CompanyData") or die(mysql_error());
?>
Retrieving Data from the table requires that you retrieve the data, store it in a variable, and then print it out. To do this for "x" number of rows in a database, you need to put it into a loop so that it executes multiple times (otherwise you get the first and only the first row in your results). This big code block would look something like:
<?php
// Connects to the Employee Database
mysql_connect("my.company.com", "admin", "getmein") or die(mysql_error());
mysql_select_db("CompanyData") or die(mysql_error());
?>
<?php
// Connects to the Employee Database
mysql_connect("my.company.com", "admin", "getmein") or die(mysql_error());
mysql_select_db("CompanyData") or die(mysql_error());
// Create a SQL query to select all records from the database and store it in the variable data
$data = mysql_query("SELECT * FROM Employees")
or die(mysql_error());
// Print a table with headings in which to display the results
Print "<table border cellpadding=3>";
Print "<tr>";
Print "<th>First Name:</th>";
Print "<th>Last Name:</th>"
Print "<th>Gender:</th>"
Print "<th>Department:</th></tr>"
// Start the loop that says while there is something in the SQL results, keep performing these instructions
while($info = mysql_fetch_array( $data ))
{
Print "<tr>";
Print "<td>".$info['FirstName'] . "</td> ";
Print "<td>".$info['LastName'] . " </td>
Print "<td>".$info['Gender'] . " </td>
Print "<td>".$info['Department'] . " </td></tr>";
}
//Close out the table code when all the data is done
Print "</table>";
?>
The comments I put in the code are pretty self explanatory. The only real missing piece is the mysql_fetch_array($data)section. What that does is fetch the SQL results which were stored in the $data variable and put them into an array. Needless to say you can expand on the statement
$data = mysql_query("SELECT * FROM Employees")
or die(mysql_error());
To create much more complex queries that use "where" statements to limit results to specific criteria which you can either hardcode in your SQL statement or obtain from a web form.
Hope these additional code snippets and understanding help you along your way into PHP. It is most definitely here to stay and is a worthwhile addition to your programming toolkit.
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Intranet Journal's Tutorials |
|
Managing Editor |