PHP, MySQL and the PEAR Database - PEAR error reporting
(Page 4 of 4 )
The function DB::isError will check to see whether the result that's been returned to you is an error or not. If it is an error, you can use DB::errorMessage to return a text description of the error that was generated. You need to pass DB::errorMessage the return value from your function as an argument.
Here you rewrite the PEAR code to use error checking:
<?php
if ( DB::isError( $demoResult = $db->query( $sql)))
{
echo DB::errorMessage($demoResult);
} else {
while ($demoRow = $demoResult->fetchRow()) {
echo $demoRow[2] . '<br />';
}
}
?>
Now that you have a good handle on connecting to the database and the various functions of PEAR, we're going to talk about forms. Forms provide a way to send substantial data from the user to the server where it can be processed.
Chapter 9 Questions
Question 9-1. Create a PEAR-style connect string to connect to this database:
hostname: oreilly.com
database name: survey
username: joe
password: my$ql
Question 9-2. Using the parameters in Question 9-1, write the non-PEAR PHP code to connect to a database and select the instance.
Question 9-3. Using the connection from Question 9-2, write the non-PEAR PHP code to fetch and display the results of the query select * from authors;.
Question 9-4. What are the advantages of using PEAR?
See the Appendix for the answers to these questions.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
|
This article is excerpted from chapter 9 of Learning PHP and MySQL, written by Michele Davis and Jon Phillips (O'Reilly, 2006; ISBN: 0596101104). Check it out today at your favorite bookstore. Buy this book now.
|
|