Previous Section  < Day Day Up >  Next Section

12.6 Exercises

  1. This program has a syntax error in it:

    <?php
    
    $name = 'Umberto';
    
    function say_hello( ) {
    
        print 'Hello, ';
    
        print global $name;
    
    }
    
    say_hello( );
    
    ?>

    Without running the program through the PHP interpreter, try to figure out what the parse error looks like that gets printed when the interpreter tries to run the program. What change must you make to the program to get it to run properly and print Hello, Umberto?

  2. Modify the validate_form( ) function in your answer to Exercise 6.3 so that it prints in the web server error log the names and values of all of the submitted form parameters.

  3. Modify your answer to Exercise 7.4 to use a custom database error-handling function that prints out different messages in the web browser and in the web server error log. The error-handling function should make the program exit after it prints the error messages.

  4. This program is supposed to print out an alphabetical list of all the customers in the table from Exercise 7.4. Find and fix the errors in it.

    <?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 objects
    
    $db->setFetchMode(DB_FETCHMODE_OBJECT);
    
    // 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->getAll('SELECT ** FROM customers ORDER BY phone DESC');
    
    if ($customers->num_rows( ) = 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>%f</td><td>%s</td></tr>',
    
                   $customer['customer_id'],
    
                   htmlentities($customer['cutsomer_name']),
    
                   $customer['phone'],
    
                   $customer['favorite_dish_id']);
    
        }
    
        print '</table>';
    
    ?>

    Previous Section  < Day Day Up >  Next Section