CS200: Computer Science, Spring 2002
|
Notes: 10 April 2002
Schedule
- Monday, 15 April: Problem Set 8 (Proposals)
- Monday, 22 April: Problem Set 8 (Progress Meetings)
- Monday, 29 April: Problem Set 8 (Final)
HTML/PHP/SQL Example View generated page: presidents.php3
<html> <head><title>Presidents of the United States</title></head> <body> <h1>Presidents of the United States</h1> <p> <? // connection information $hostName = "dbm1.itc.virginia.edu"; $userName = "dee2b"; $password = "quist"; $dbName = "dee2b_presidents"; // make connection to database mysql_connect($hostName, $userName, $password) or exit("Unable to connect to host $hostName"); mysql_select_db($dbName) or exit("Database $dbName does not exist on $hostName"); // Run a SQL Query $result = mysql_query("SELECT lastname, firstname FROM presidents " . "WHERE college='William and Mary' ORDER BY lastname"); // Determine the number of rows $numrows = mysql_num_rows($result); $numcols = mysql_num_fields($result); // print the results print "<table border=0 cellpadding=5>"; print "<tr>"; for ($k = 0; $k < $numcols; $k = $k + 1) { print "<th>" . mysql_field_name ($result, $k) . "</th>"; } print "</tr>"; for ($i = 0; $i < $numrows; $i++) // $i++ is short for $i = $i + 1 { for ($j = 0; $j < $numcols; $j++) { print "<td>" . mysql_result ($result, $i, $j) . "</td>"; } print "</tr>"; } print "</table>"; mysql_close(); // Close the database connection ?> <p> <hr> <a href="mailto:evans@cs.virginia.edu"><em>evans@cs.virginia.edu</em></a> </body> </html>
University of Virginia Department of Computer Science CS 200: Computer Science |
David Evans evans@virginia.edu Using these Materials |