使用两个不同的查询处理所有行

时间:2016-04-09 17:48:16

标签: php

有两种不同的查询。

select * from blockinfo where book='$cod' and user='$user'
select * from blockinfo where user='$user' and book='$cod' and flag!=0"

这两个查询将在example.comexample.com?cod=value这样的网页上执行。 我必须处理所有行的第一个查询,然后是第二个查询。 我必须使用两个while循环进行处理。

我可以使用单循环处理两个查询吗?

if($quant!=NULL&&$cod!=NULL&&$user!=NULL&&$quantdb!=NULL&&$title!=NULL){
  $sql="select * from blockinfo where book='$cod' and user='$user' ";
  $res=mysqli_query($con,$sql);
  $numrows=mysqli_num_rows($res);
  if($numrows>0){
      while($row=mysqli_fetch_assoc($res)){
      $flag=$row['flag'];
      date_default_timezone_set('Asia/Kolkata');
      $current_time=date('Y/m/d H:i:s');
      //echo "$current_time   ";
      $current_time=strtotime($current_time);

      $timestamp=strtotime($time)+1*1*60;
      $collect_time=date('Y/m/d H:i:s',$timestamp);
      //echo "$collect_time ";
      $collect_time=strtotime($collect_time);

      $difference=$collect_time-$current_time;
      $days=floor($difference/86400);
      $difference=($difference%86400);
      $hrs=floor($difference/3600);
      $difference=($difference%3600);
      $mins=floor($difference/60);
      $difference=($difference%60);
      $seconds=floor($difference);
          if($days<=0&&$hrs<=0&&$mins<=0&&$seconds<=0){
              if($flag==0)
                  echo "you did not collect the book in time <br/>";
              if($flag==1){
                  $upd="update books set quantity=quantity+'$quant' where code='$cod' ";
                  mysqli_query($con,$upd);
                  $flag=0;
                  $upd2="update blockinfo set flag=0 where book='$cod' and user='$user' and blockedtime='$time'";
                  mysqli_query($con,$upd2);
              }
              if($flag==2){
                  $del="delete from blockinfo where book='$cod' and user='$user' and blockedtime='$time'";
                  mysqli_query($con,$del);
              }
          }
          else if($flag==1){
              echo "you have blocked $quant books of name $title <br/>";
              echo "you have $days days $hrs hours $mins minutes $seconds seconds left to collect your book<br/>";
          }
      }
  }

//do something

if($cod==NULL)
$sql="select * from blockinfo ";
      $res=mysqli_query($con,$sql);
      $numrows=mysqli_num_rows($res);
      if($numrows>0){
          while($row1=mysqli_fetch_assoc($res)){
              $resbook=$row1['book'];
              $blockedtime=$row1['blockedtime'];
              $user=$row1['user'];
              $quant=$row1['quant'];
              $flag=$row1['flag'];
              date_default_timezone_set('Asia/Kolkata');
              $current_time=date('Y/m/d H:i:s');
              //echo "$current_time   ";
              $current_time=strtotime($current_time);

              $timestamp=strtotime($blockedtime)+1*1*60;
              $collect_time=date('Y/m/d H:i:s',$timestamp);
              //echo "$collect_time ";
              $collect_time=strtotime($collect_time);
              //echo "$flag";
              $difference=$collect_time-$current_time;
              $days=floor($difference/86400);
              $difference=($difference%86400);
              $hrs=floor($difference/3600);
              $difference=($difference%3600);
              $mins=floor($difference/60);
              $difference=($difference%60);
              $seconds=floor($difference);
              if($days<=0&&$hrs<=0&&$mins<=0&&$seconds<=0){
                if($flag==1){
                    $upd="update books set quantity=quantity+'$quant' where code='$resbook' ";
                    mysqli_query($con,$upd);
                    $flag=0;
                    $upd2="update blockinfo set flag=0 where book='$resbook' and user='$user' and blockedtime='$blockedtime'";
                    mysqli_query($con,$upd2);
                }
                if($flag==2){
                    $del="delete from blockinfo where book='$resbook' and user='$user' and blockedtime='$blockedtime'";
                    mysqli_query($con,$del);
                }
              }
          }
      }

0 个答案:

没有答案