Course Notes:
Chapter 10:
Some standard code to connect to a database via mysql and to print the entire contents of the table.
<html>
<head>
<title>Show DATA </title>
</head>
<body>
<h1>Show Database </h1>
<?
//make the database connection
$conn = mysql_connect("localhost", "username", "password");
mysql_select_db("data_base_name", $conn);
//create a query
$sql = "SELECT * FROM table_name ";
$result = mysql_query($sql, $conn);
print "<table border = 1>\n";
//get field names
print "<tr>\n";
while ($field = mysql_fetch_field($result)){
print " <th>$field->name</th>\n";
} // end while
print "</tr>\n\n";
//get row data as an associative array
while ($row = mysql_fetch_assoc($result)){
print "<tr>\n";
//look at each field
foreach ($row as $col=>$val){
print " <td>$val</td>\n";
} // end foreach
print "</tr>\n\n";
}// end while
print "</table>\n";
?>
</body>
</html>
Now connect with an HTML file to the PHP file with form sending password.
HTML FILE:
<html>
<head>
<title>Show DATA</title>
</head>
<body>
<h1>Show CS334</h1>
<?php
$pword=0;
$uname = $_REQUEST["uname"];
$pword = $_REQUEST["pword"];
if($pword=="7777777" && $uname=="root")
{
{
//make the database connection
$conn = mysql_connect("localhost", "$uname", "$pword");
mysql_select_db("cs334_classmates", $conn);
//create a query
$sql = "SELECT * FROM roster";
$result = mysql_query($sql, $conn);
print "<table border = 1>\n";
//get field names
print "<tr>\n";
while ($field = mysql_fetch_field($result)){
print " <th>$field->name</th>\n";
} // end while
print "</tr>\n\n";
//get row data as an associative array
while ($row = mysql_fetch_assoc($result)){
print "<tr>\n";
//look at each field
foreach ($row as $col=>$val){
print " <td>$val</td>\n";
} // end foreach
print "</tr>\n\n";
}// end while
print "</table>\n";
}
}
else
{
print "YOU DUMMY, WRONG LOGIN!";
}
?>
</body>
</html>
Now Query JUST THE Sr Class!:
PHP file:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd">
<!-- saved from url=(0069)file://\\ad.cs.pitt.edu\Users\Users1\dmb\Desktop\showCS334_pwd_sr.htm -->
<HTML><HEAD><TITLE>connect</TITLE>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
<META content="MSHTML 6.00.6000.16640" name=GENERATOR></HEAD>
<BODY>
<P>To display the database table contents enter you name and password below:</P>
<FORM name=form1 action=showCS334_pwd_class.php method=post>
<P>User Name: <INPUT name=uname> </P>
<P>Password <INPUT name=pword> </P>
<P>Class
<input type="text" name="class">
</P>
<P><INPUT type=submit value=Submit name=Submit> </P>
<P> </P></FORM>
<P> </P>
<P> </P></BODY></HTML>
PHP file:
<html>
<head>
<title>Show DATA</title>
</head>
<body>
<h1>Show CS334</h1>
<?php
$pword=0;
$uname = $_REQUEST["uname"];
$pword = $_REQUEST["pword"];
$class = $_REQUEST["class"];
if($pword=="7777777" && $uname=="root")
{
{
//make the database connection
$conn = mysql_connect("localhost", "$uname", "$pword");
mysql_select_db("cs334_classmates", $conn);
//create a query
$sql = "SELECT * FROM roster WHERE class='$class' ";
$result = mysql_query($sql, $conn);
print "<table border = 1>\n";
//get field names
print "<tr>\n";
while ($field = mysql_fetch_field($result)){
print " <th>$field->name</th>\n";
} // end while
print "</tr>\n\n";
//get row data as an associative array
while ($row = mysql_fetch_assoc($result)){
print "<tr>\n";
//look at each field
foreach ($row as $col=>$val){
print " <td>$val</td>\n";
} // end foreach
print "</tr>\n\n";
}// end while
print "</table>\n";
}
}
else
{
print "YOU DUMMY, WRONG LOGIN!";
}
?>
</body>
</html>
|