C.11 Chapter 12
C.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:
Line 5: Two colons are needed between DB and
connect. Lines 9 and 10: The fetch mode should be set to
DB_FETCHMODE_ASSOC since rows are treated as
arrays in the rest of the program. (Alternatively, you could change
lines 15 and 25-28 so that they treat rows as objects.) Line 15: There is an extra closing square bracket after
$row['dish_id']. Line 17: This should be a call to $db->query(
), not $db->getAll( ), because
fetchRow( ) is used in line 23 to retrieve each
row. The SQL query is also wrong: it should be SELECT * FROM
customers ORDER BY customer_name (only one asterisk after
SELECT and customer_name, not
phone DESC, after ORDER BY). Line 18: The method name that returns the number of rows retrieved by
query( ) is numRows( ), not
num_rows( ). Line 22: The string has mismatched delimiters. Either change the
opening quote to a double quote or the closing quote to a single
quote. Line 26: The array key is misspelled. It should be
customer_name, not
cutsomer_name. Line 28: $customer['favorite_dish_id'] is the
integer ID of the favorite dish. To display the dish name, you need
to look up the appropriate element in $dish_names.
Instead of $customer['favorite_dish_id'], it
should be $dish_names[ $customer['favorite_dish_id']
]. Line 31: The curly brace to end the else code
block is missing.
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>';
}
?>
|