Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在php中创建循环_Php_Mysql - Fatal编程技术网

如何在php中创建循环

如何在php中创建循环,php,mysql,Php,Mysql,我需要从一个给定的数据范围内选择名为booked的表中的数据,并从一个变量中显示数据,然后使用它从另一个表中选择并显示所选的数据,但当从booked表中选择的数据为多个时,变量中仅显示第一个数据以下是我的代码: $res1=mysqli_query($bd,"select * from booked where datefrom between '$from' and '$to' or dateto>='$from' and dateto='$to'"); $num1=mysqli_nu

我需要从一个给定的数据范围内选择名为booked的表中的数据,并从一个变量中显示数据,然后使用它从另一个表中选择并显示所选的数据,但当从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>';
        }
     }
 }
$res1=mysqli_query($bd,“选择*从预订的日期,其中日期介于'$from'和'$to'之间,或日期到>='$from'和日期到='$to');
$num1=mysqli_num_行($res1);
如果($num1>0)
{

对于($y=0;$y您可能希望尝试使用不同的方法,因为它不太容易引用错误,下面是一个完整的示例,使用
herdoc
循环
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);
?>

对不起,我只是一个初学者,你能帮我写代码吗?你应该解释一下你想让代码做什么,而粘贴的代码没有做到。这是一个度假酒店预订系统,就是表单预订。我需要做到的是,如果有人已经预订了他/她选择的日期,房间将隐藏在我能看到你的地方避免在循环内循环。