MySQL中缺少第一行数据 [英] Missing first row of data from MYSQL

查看:68
本文介绍了MySQL中缺少第一行数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个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打印示例2

推荐答案

从代码中删除第21行:

$row=mysqli_fetch_assoc($result2);

它在while循环提取记录的睡觉之前提取第一行

为什么?

while循环之外,您调用了第一行,但没有打印它。输出中缺少此行。

这篇关于MySQL中缺少第一行数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆