forward one row through the array that resulted from the query. This happens until the pointer reaches
the end of the result set, whereupon it can't be moved on any more, and you get a FALSE result. $row
thus becomes FALSE, you exit the while loop, and you're done.
This basic functionality is fairly straightforward but should be sufficient to perform almost any query on a
MySQL database. Just a small set of PHP instructions can be sufficient to create a truly dynamic
experience for people visiting your database-driven Web site.
Summary
Today, you paid a brief visit to the world of PHP. You learned how PHP scripts can be embedded in HTML
Web pages, and looked at how to write basic PHP code.
You learned how to use a range of PHP's control structures, such as includes, ifs, while, and for
loops. Although you have by no means explored the PHP vocabulary to any great extent, you have
learned a basic toolkit with which you can write powerful scripts.
You went on to examine the MySQL API, with its basket of functions for accessing a MySQL server.
You learned how to connect to a MySQL server, create databases and tables, insert data, and handle
errors.
Finally, you looked at running MySQL SELECT commands via PHP. Results are returned in array form,
and you learned some commands for handling and processing the results so that they can be retrieved
and presented to the client in a useful form.
Q&A
Q: How do I learn more about PHP?
A:
You should visit http://www.php.net/ and read other Sams titles on the
subject. You will also need a Web server onto which you can install PHP,
although you should find it free to download and easy to set up.
Q: Why should I use PHP rather than another language?
A:
There's a good choice of server-side languages and tools on the market.
PHP has the advantage that it's free, fast, and can be installed on the most
popular platforms.
PHP is also easy to write; you merely have to insert some PHP code
into a Web page to create a PHP script.
Exercises
- Write the PHP syntax doing a SELECT on a table called products. The table
includes the fields name and price. Your query should retrieve all products whose
price is less than $50. You should allow for an error condition and, if need be, call
the user-defined error_report function. - Write the PHP function for connecting to a MySQL database on localhost and
returning the link identifier to the calling program.
Day 16: MySQL and Time
Overview
Today, you will look at how MySQL handles time. You will understand the formats that MySQL uses to
represent dates and time, and learn how to use the special functions MySQL has that help you handle date
and time formatting and arithmetic.
How Dates Are Treated in MySQL
MySQL has a range of data types for handling date and time information. In general, MySQL will accept
several formats when being given data to put into a field. However, date and time output will always be
standardized and appear in a predictable format.