我无法使用
的循环变量$counter
$HA_Row = mysql_fetch_array($HA_Res);
表示我将获得哪一行数据。基本上,我不知道一起使用$counter
和$HA_Res
。
我想它可以显示不同的行'我的数据库的数据。
例如,像
echo "$HA_Row[Check_In_Date]"
我试过了
echo "$HA_Row[$counter][Check_In_Date]"
和
echo "$HA_Row[Check_In_Date][$counter]", echo "[$counter]$HA_Row[Check_In_Date]"
但它们都不起作用。
非常感谢你!感谢你的时间。
<?php
session_start();
include_once 'dbconnect.php';
if(!isset($_SESSION['Hotel_User_ID']))
{
header("Location: index.php");
}
$res = mysql_query("SELECT * FROM Hotel_Info WHERE Hotel_ID =".$_SESSION['Hotel_User_ID']);
$userRow = mysql_fetch_array($res);
$HA_Res = mysql_query("SELECT * FROM Hotel_Account WHERE Hotel_ID =".$_SESSION['Hotel_User_ID']);
$HA_Row = mysql_fetch_array($HA_Res);
$HA_Count = mysql_num_rows($HA_Res);
// echo "$HA_Count". "<br>";
// echo "$HA_Row[Check_In_Date]";
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Hello - <?php echo $userRow['Hotel_Email']; ?></title>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<div id="header">
<div id="left">
<label>Welcome <?php echo $userRow['Hotel_Uname']; ?> </label>
</div>
<div id="right">
<div id="content">
<a href="logout.php?logout">Sign Out</a>
</div>
</div>
</div>
<div id="menu">
<a href="manage.php"> Manage </a> <br>
<a href="home.php"> Update </a>
</div>
<div id="body">
<center>
<div id="Hotel-InfoForm">
<form method="post" enctype="multipart/form-data">
<!-- <table.ver2 align="center" width="60%" border="0"> -->
<table align="center" width="85%" border = "1">
<tr>
<th>Check In Data</th>
<th>Check Out Data</th>
<th>Theme</th>
<th>Bed Size</th>
<th>First Name</th>
<th>Last Name</th>
<th>Gender</th>
<th>Age</th>
<th>Phone Number</th>
<th>Email</th>
</tr>
<?php
for($counter = 0; $counter < $HA_Count; $counter = $counter + 1)
{
// using $counter for $HA_Row[....]
echo "<tr>";
echo "<td>";
echo "$HA_Row[Check_In_Date]";
echo "</td>";
echo "<td>";
echo "$HA_Row[Check_Out_Date]";
echo "</td>";
$Theme_Res = mysql_query("SELECT * FROM Theme WHERE Theme_ID = ".$HA_Row[Theme_ID]);
$Theme_Row = mysql_fetch_array($Theme_Res);
echo "<td>";
echo "$Theme_Row[Theme_Name]";
echo "</td>";
echo "<td>";
echo "$Theme_Row[Bed_Size]";
echo "</td>";
$Guest_Res = mysql_query("SELECT * FROM Guest_Info WHERE Guest_ID = ".$HA_Row[Guest_ID]);
$Guest_Row = mysql_fetch_array($Guest_Res);
echo "<td>";
echo "$Guest_Row[First_Name]";
echo "</td>";
echo "<td>";
echo "$Guest_Row[Last_Name]";
echo "</td>";
echo "<td>";
echo "$Guest_Row[Gender]";
echo "</td>";
function ageCalculator($dob)
{
if(!empty($dob))
{
$birthdate = new DateTime($dob);
$today = new DateTime('today');
$age = $birthdate->diff($today)->y;
return $age;
}
else
{
return 0;
}
}
echo "<td>";
echo ageCalculator($Guest_Row[Birth_Day]);
echo "</td>";
echo "<td>";
echo "$Guest_Row[Phone_Num]";
echo "</td>";
echo "<td>";
echo "$Guest_Row[Email]";
echo "</td>";
echo "</tr>\n";
}
?>
</table>
</form>
</div>
</center>
</div>
</body>
</html>
答案 0 :(得分:1)
您可以使用mysql_fetch_assoc和while循环,如下所示:
while($HA_Row = mysql_fetch_assoc($HA_Res)){
echo $HA_Row[Check_In_Date]."<br>";
};
这会在每个实例中循环!
答案 1 :(得分:1)
您通常不会使用计数器来控制fetch
,特别是如果您只是想按顺序处理记录。每次调用fetch
都会返回结果集中的 next 行,而无需您采取进一步操作。
首先,将所有mysql_*
次来电替换为mysqli_*
次来电。然后替换你的行
for($counter = 0; $counter < $HA_Count; $counter = $counter + 1)
与
while ($HA_Row = mysqli_fetch_array($HA_Res))
在每个循环中,mysqli_fetch_array
返回 next 行,在阅读完最后一行后,下一个调用将返回 false
,退出循环。
切换为mysqli_*
或PDO
也可以获得准备好的陈述,这有助于防范SQL injection以及众多其他好处。