<HTML>
<HEAD>
<TITLE> CS107 Using MySQL with PHP </TITLE>
</HEAD>

<BODY>

<?PHP
    
// Open a connection to the desired database.
    
$db = mysql_connect( 'localhost', 'yourusername', 'yourpassword' );
    
mysql_select_db( 'yourusername', $db );
?>


<H3> The Query </H3>

<?PHP
    
// Build a long query out of its smaller pieces.

    
$query = "SELECT authors.first_name, authors.last_name, books.title
                FROM authors, books 
                WHERE authors.auth_id = books.author_id AND authors.last_name = '
$author'";

    echo 
"<P>$query</P>\n";
?>


<H3> Search Results </H3>

<?PHP
    
// Send the query to the database.

    
$result = mysql_query( $query, $db );


    
// Use result to extract and print the book titles and title, one
    // book at a time (with a separate "mysql_fetch_row" for each book).

    
$match = mysql_fetch_row( $result );
    while( 
$match )
    {
        
// $match is now a 3-item array containing the author's first
        // name, last name, and book title.

        
echo "<P>$match[2], by $match[0] $match[1]</P>\n";


        
// Fetch the next row of the query result.  The
        // while-loop will stop when there are no more rows.

        
$match = mysql_fetch_row( $result );
    }

    
mysql_close( $db );
?>

</BODY>
</HTML>