// This is included to hide the username and password (not in the book)
Source code example for Web Database Applications
Unless otherwise stated, the source code distributed with this book can be
redistributed in source or binary form so long as an acknowledgment appears
in derived source files.
The citation should list that the code comes from Hugh E.
Williams and David Lane, "Web Database Application with PHP and MySQL"
published by O'Reilly & Associates.
This code is under copyright and cannot be included in any other book,
publication, or educational product without permission from O'Reilly &
No warranty is attached; we cannot take responsibility for errors or fitness
<!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.0 Transitional//EN"
die("Error " . mysql_errno() . " : " . mysql_error());
// (1) Open the database connection
// In the book, $username is replaced with "fred"
// and $password is replaced with "shhh"
if (!($connection = @ mysql_connect("localhost",$username,$password)))
die("Could not connect");
// NOTE : 'winestore' is deliberately misspelt to
// cause an error
if (!(mysql_select_db("winestor", $connection)))
// (2) Run the query on the winestore through the
if (!($result = @ mysql_query ("SELECT * FROM wine", $connection)))
// (3) While there are still rows in the result set,
// fetch the current row into the array $row
while ($row = mysql_fetch_row($result))
// (4) Print out each element in $row, that is,
// print the values of the attributes
for ($i=0; $i<mysql_num_fields($result); $i++)
echo $row[$i] . " ";
// Print a carriage return to neaten the output
// (5) Close the database connection