php - 如何在php中制作循环

标签 php mysql

我需要从给定范围的数据中从名为 booked 的表中选择数据并显示数据形成一个变量,然后用它从另一个表中选择并显示所选的数据但是当从表中选择的数据已预订是多个只有第一个数据显示在变量中这是我的代码:

$res1=mysqli_query($bd,"select * from booked where  datefrom between '$from' and '$to' or dateto>='$from' and dateto='$to'");
$num1=mysqli_num_rows($res1);
if($num1>0)
{
   for($y=0;$y<$row1=mysqli_fetch_assoc($res1);$y++)
   {
       $res=mysqli_query($bd,"select * from rooms where capacity>='$newcap' and room_number!='".$row1['roomnumber']."'");
       while($row=mysqli_fetch_assoc($res))
       {
          echo'<div class="col-lg-4 col-md-4 col-sm-12">';
                echo'<div class="newsBox">
                    <div class="thumbnail">
                        <figure><img src="reservation/img/rooms/'.$row['img'].'"  width="230" height="150"></figure>
                        <div class="caption maxheight2">
                        <div class="box_inner">
                                    <div class="box">
                                        <a class="title"><strong>'.$row['name'].'</strong></p>
                                        <b>'.$row['description'].'</b>
                                        <p>'.$row['price'].'</p>
                                    </div>
                                  <a class="btn btn-default" href="info_pay.php?roomnumber='.$row['room_number'].'&roomtype='.$row['name'].'&from='.$_POST['from'].'&adult='.$_POST['adult'].'&child='.$_POST['child'].'&to='.$_POST['to'].'&roomprice='.$row['price'].'"><span class="glyphicon glyphicon-plus">Select this Room</span></a>
                            </div>
                        </div>
                    </div>
                </div>';
            echo'</div>';
        }
     }
 }

最佳答案

您可能想使用 heredoc 尝试不同的方法因为它不太容易出现引用错误,下面是一个使用 heredoc 循环 mysqli 查询的完整示例。

<?php

$con=mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$sql="select * from booked where  datefrom between '$from' and '$to' or dateto>='$from' and dateto='$to'";

if ($result=mysqli_query($con,$sql))
  {
  while ($row=mysqli_fetch_row($result))
    {
echo <<< LOL
        <div class="col-lg-4 col-md-4 col-sm-12">
                <div class="newsBox">
                    <div class="thumbnail">
                        <figure><img src="reservation/img/rooms/{$row['img']}"  width="230" height="150"></figure>
                        <div class="caption maxheight2">
                        <div class="box_inner">
                                    <div class="box">
                                        <a class="title"><strong>{$row['name']}</strong></p>
                                        <b>{$row['description']}</b>
                                        <p>{$row['price']}</p>
                                    </div>
                                  <a class="btn btn-default" href="info_pay.php?roomnumber={$row['room_number']}&roomtype={$row['name']}&from={$_POST['from']}&adult={$_POST['adult']}&child={$_POST['child']}&to={$_POST['to']}&roomprice={$row['price']}"><span class="glyphicon glyphicon-plus">Select this Room</span></a>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
LOL;

  // Free result set
  mysqli_free_result($result);
}

}
//close mysqli connection
mysqli_close($con);
?>

关于php - 如何在php中制作循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33086103/

相关文章:

php - 使用android volley post请求从MySQL中选择数据并将其显示在recyclerview中

php - 用常量定义表和列名

php - mysql内连接只得到最后3个等于

mysql - 从 3 个表查看

php - 文件上传限制为 1MB?

php - 使用javascript更新sql记录

mysql - 无法删除 UPDATE 查询中的 SQL 语法

python - 无法将数据插入数据库,但每次运行它都会返回成功

MYSQL 用值填充外部 id

PHP 仅在命令行模式下抛出 XDebug 错误