$db = mysql_connect("localhost", "root");
mysql_select_db("mydb",$db);
$result = mysql_query("SELECT * FROM employees",$db);
if ($myrow = mysql_fetch_array($result)) {
do {
printf("
%s %sn",
$PATH_INFO, $myrow["id"], $myrow["first"], $myrow["last"]);
} while ($myrow = mysql_fetch_array($result));
} else {
echo "对不起,没有找到记录!";
}
?>
$db = mysql_connect("localhost", "root");
mysql_select_db("mydb",$db);
// display individual record
// 显示单条记录内容
if ($id) {
$result = mysql_query("SELECT * FROM employees WHERE id=$id",$db);
$myrow = mysql_fetch_array($result);
printf("名: %sn
", $myrow["first"]);
printf("姓: %sn
", $myrow["last"]);
printf("住址: %sn
", $myrow["address"]);
printf("职位: %sn
", $myrow["position"]);
} else {
// show employee list
// 显示员工列表
$result = mysql_query("SELECT * FROM employees",$db);
if ($myrow = mysql_fetch_array($result)) {
// display list if there are records to display
// 如果有记录,则显示列表
do {
printf("
%s %sn", $PATH_INFO,
$myrow["id"], $myrow["first"], $myrow["last"]);
} while ($myrow = mysql_fetch_array($result));
} else {
// no records to display
// 没有记录可显示
echo "对不起,没有找到记录!";
}
}
?>
程序开始变得复杂了,所以我在这里面加了注释,来解释一下到底发生了什么。您可以用//加入单行注释,或者用/*和*/来括住大段的注释。