【发布时间】:2018-09-15 14:20:29
【问题描述】:
我编写了一个 php 脚本,在其中返回餐厅名称、地址、电话号码、营业时间表和自定义菜单链接。但是,即使在数据库中有一个星期一小时的条目,当我在 mysqli_fetch_assoc 中执行 while 循环时,它也不会出现。这是我的代码:
<?php
session_start();
$con=mysqli_connect("root","");
$rest_id2=$_GET['id'];
$rest_id=(int)$rest_id2;
var_dump($rest_id);
$sql="SELECT * FROM restaurant WHERE restaurant_id='".$rest_id."'";
$result=mysqli_query($con,$sql);
$rows=mysqli_fetch_assoc($result);
echo '<strong>'. "Restaurant name:". '</strong><br><br>';
echo $rows['restaurant_name'];
echo "<br><br>";
echo "<strong>Address: </strong><br><br>";
echo $rows['address_1']." ".$rows['address_2']." ". $rows['city']. ", ".
$rows['state']. " ". $rows['zip']. "<br><br>";
echo '<strong>'. "Phone number:". '</strong><br><br>';
echo $rows['phone_number']. "<br><br>";
//hours table
$sql2="SELECT * from hours WHERE restaurant_id='".$rest_id."'";
$result2=mysqli_query($con,$sql2);
$row=mysqli_fetch_assoc($result2);
echo "<table border='1' cellpadding='10'><tr><th>Open or Closed</th> .
<th>Day</th><th>Start Time</th><th>End Time</th></tr>";
$num_rows=mysqli_num_rows($result2);
// var_dump($num_rows);
while($row=mysqli_fetch_assoc($result2)){
// var_dump($rows);
if ($num_rows==0){
echo "No hours data available";
}
elseif($row['day']=="Closed"){
echo "<td><strong>". $row['day']. "</td></strong><br>";
echo "<td><strong>". $row['open_closed']. "</td></strong><br>";
echo "<td><strong>". "-". "</td></strong><br>";
echo "<td><strong>". "-". "</td></tr></strong><br>";
}
else{
echo "<tr><td><strong>". $row['day']. "</td></strong><br>";
echo "<td><strong>". $row['open_closed']. "</td></strong><br>";
echo "<td><strong>". $row['start_time']. "</td></strong><br>";
echo "<td><strong>". $row['end_time']. "</td></tr></strong><br>";
}
}
echo '<a href="' . "custom_menu.php?id=" .$rows['restaurant_id']. '"'.
'>'."<strong>Menu specialized for you</strong>" . '<br>'. '</a>';
?>
还提供了我在网站上看到的内容。有谁知道为什么会这样?
【问题讨论】:
-
不看表和表结构就很难回答
-
刚刚添加了表结构和通过php打印出来的内容
-
也许周一的菜单有过敏原。只是猜测,因为我不知道那些桌子上有什么:D
-
抱歉,我从错误的脚本上传了代码!现在是正确的代码
-
注意:试着改掉在一次性变量中声明 SQL 语句的习惯,这些变量只使用一次。在将查询直接提供给函数的情况下,跟踪代码要容易得多,并且不再有可能搞砸并发送
$sql3而不是视觉上相似的$sql8。
标签: php mysql mysqli fetch associative-array