You are on page 1of 2

thiet ke web - thiet ke website - thiet ke web - Design A website

Bi 4: Hin th d liu trong Database ln mn hnh


ng ngy: 20-10-2009Ln xem: 12950

Hin th d liu trong Database ln mn hnh


By gi d liu c ghi vo CSDL guestbook . Bn cn thc hin vic xem cc d liu
. Nn nh l chng ta li phi s dng dbconnect.php nh ti ni vi bn trc y.
Bn cn phi cho hin th tt c cc record trong table thng tin ca khch ving thm
nhp vo. Chng ta thc hin script sau v t tn l view.php t th mc gc:

<?php include("dbconnect.php"); ?>


<h2>View My Guest Book!!</h2>
<?php
$result = mysql_query("select * from guestbook") or die (mysql_error());
while ($row = mysql_fetch_array($result))
{
echo "<b>Name:</b>";
echo $row["name"];
echo "<br>\n";
echo "<b>Location:</b>";
echo $row["location"];
echo "<br>\n";
echo "<b>Email:</b>";
echo $row["email"];
echo "<br>\n";
echo "<b>URL:</b>";
echo $row["url"];
echo "<br>\n";
echo "<b>Comments:</b>";
echo $row["comments"];
echo "<br>\n";
echo "<br>\n";
echo "<br>\n";
}
mysql_free_result($result);
?>
<h2><a href="sign.php">Sign My Guest Book!!</a></h2>

Bn g i ch http://localhost/view.php xem kt qu.

Nh chng ta thy query trong MySQL truy cp tt c cc hng trong database. Script thc
hin vic ny bng cch s dng vng lp thng qua bin $row.

Trong mi vng lp th mi field trong tng record c hin th. Vd: print $row["email"] s
ghi ra mn hnh i vi record ang truy cp. Khi chy chng trnh, tt c cc field ca
tng record s c hin th.
page 1 / 2

thiet ke web - thiet ke website - thiet ke web - Design A website

Ni chung n y bn hon thnh mt ng dng c bn v php, tuy nhin upload ng


dng ny ln interner mnh cn phi chnh sa thm nhiu.
admin

page 2 / 2

You might also like