< Day Day Up > |
C.11 Chapter 12C.11.1 Exercise 1:The error message looks like: Parse error: parse error, unexpected T_GLOBAL in exercise-12-1.php on line 6 The global declaration has to be on a line by itself, not inside the print statement. To fix the program, separate the two: <?php $name = 'Umberto'; function say_hello( ) { global $name; print 'Hello, '; print $name; } say_hello( ); ?> C.11.2 Exercise 2:function validate_form( ) { $errors = array( ); // Capture the output of var_dump( ) with output buffering ob_start( ); var_dump($_POST); $vars = ob_get_contents( ); ob_end_clean( ); // Send the output to the error log error_log($vars); // operand 1 must be numeric if (! strlen($_POST['operand_1'])) { $errors[ ] = 'Enter a number for the first operand.'; } elseif (! floatval($_POST['operand_1']) = = $_POST['operand_1']) { $errors[ ] = "The first operand must be numeric."; } // operand 2 must be numeric if (! strlen($_POST['operand_2'])) { $errors[ ] = 'Enter a number for the second operand.'; } elseif (! floatval($_POST['operand_2']) = = $_POST['operand_2']) { $errors[ ] = "The second operand must be numeric."; } // the operator must be valid if (! in_array($_POST['operator'], $GLOBALS['ops'])) { $errors[ ] = "Please select a valid operator."; } return $errors; } C.11.3 Exercise 3:Change the beginning of the program to: <?php require 'DB.php'; require 'formhelpers.php'; // Connect to the database $db = DB::connect('mysql://hunter:w)mp3s@db.example.com/restaurant'); if (DB::isError($db)) { die ("Can't connect: " . $db->getMessage( )); } function db_error_handler($error) { error_log('DATABASE ERROR: ' . $error->getDebugInfo( )); die('There is a ' . $error->getMessage( )); } // Set up automatic error handling $db->setErrorHandling(PEAR_ERROR_CALLBACK,'db_error_handler'); C.11.4 Exercise 4:Here are the errors in the program:
Here is the complete corrected program: <?php require 'DB.php'; require 'formhelpers.php'; // Connect to the database $db = DB::connect('mysql://hunter:w)mp3s@db.example.com/restaurant'); if (DB::isError($db)) { die ("Can't connect: " . $db->getMessage( )); } // Set up automatic error handling $db->setErrorHandling(PEAR_ERROR_DIE); // Set up fetch mode: rows as associative arrays $db->setFetchMode(DB_FETCHMODE_ASSOC); // get the array of dish names from the database $dish_names = array( ); $res = $db->query('SELECT dish_id,dish_name FROM dishes'); while ($row = $res->fetchRow( )) { $dish_names[ $row['dish_id'] ] = $row['dish_name']; } $customers = $db->query('SELECT * FROM customers ORDER BY customer_name'); if ($customers->numRows( ) = = 0) { print "No customers."; } else { print '<table>'; print '<tr><th>ID</th><th>Name</th><th>Phone</th><th>Favorite Dish</th></tr>'; while ($customer = $customers->fetchRow( )) { printf('<tr><td>%d</td><td>%s</td><td>%s</td><td>%s</td></tr>', $customer['customer_id'], htmlentities($customer['customer_name']), $customer['phone'], $dish_names [ $customer['favorite_dish_id'] ]); } print '</table>'; } ?> |
< Day Day Up > |