Please send questions to
st10@humboldt.edu .
<html>
<!-- try_mysql3.php - see: CAN we see the pets table thus far
on sorrel?
by: Sharon Tuttle
last modified: 11-30-06
-->
<head>
<title> try_mysql3.php </title>
</head>
<body>
<h2> try_mysql3.php - view pets table </h2>
<?php
// if you have username and password already...
if ($_POST["_submit_check"])
{
$username = $_POST["username"];
$password = $_POST["password"];
$con = mysql_connect("localhost", $username, $password);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
// NOTE: on sorrel's mysql, database name is same as username
@mysql_select_db($username) or die("Unable to select database");
// try to view the contents of pets
$query = "select * ".
"from pets";
$results = mysql_query( $query, $con );
// how many rows are in my result?
$num_rows = mysql_numrows($results);
print "<h3> There are $num_rows rows in pets at this time. </h3>";
// what are those rows?
?>
<table>
<tr> <th> pet_id </th> <th> pet_name </th> <th> date of birth </th>
<th> # siblings </th> </tr>
<?php
while ($row = mysql_fetch_array($results))
{
print "<tr> <td> " . $row['pet_id'] . " </td>\n " .
" <td> " . $row['pet_name'] . " </td>\n " .
" <td> " . $row['pet_birth'] . " </td>\n " .
" <td> " . $row['num_siblings'] . " </td>\n " .
"</tr>\n";
}
print "</table>\n";
mysql_close($con);
}
else
{
print '<form method="post" action="' . $_SERVER[PHP_SELF] . '">';
?>
<table>
<tr> <td> Username: </td>
<td> <input type="text" name="username">
</tr>
<tr> <td> Password: </td>
<td> <input type="password" name="password">
</tr>
</table>
<br><br>
<input type="submit" value="Log in">
<input type="hidden" name="_submit_check" value="1">
</form>
<?php
}
?>
</body>
</head>