doujiang3997 2017-09-30 21:41
浏览 84
已采纳

pdo如何检查它是否是从数据库中检索的第一条记录?

              $sql3 = "SELECT member FROM levels where upline = ? AND level=1";
              $q3 = $conn->prepare($sql3);
              $q3->execute(array("$level2downlines"));
              while($r3 = $q3->fetch(PDO::FETCH_ASSOC)){
                      $level3downlines = $r3['member'];
                      if (it is 1st record){
                        echo "make orange juice";
                      }else{
                        echo "make all juice";
                      }
              }

Let say output are 3 records from database "Zac", "Michelle", Andrew". "Zac" is the 1st record get from database, how to write the "if" statement to check if the records is 1st record or not?

  • 写回答

2条回答 默认 最新

  • doutang6819 2017-09-30 21:51
    关注

    First off, if the order of records returned matters, you must explicitly include an ORDER BY clause in your query to specify which column to sort results on. Otherwise, the order of rows returned is technically indeterminate.

    To operate differently on the first fetched row than on later rows, you may simply call fetch() outside the loop first, then loop over the remainder. Each call to PDOStatement::fetch() will advance the rowset record pointer, so the while loop will only iterate the records after the one already retrieved.

    // Fetch the first row from the rowset
    $r3 = $q3->fetch(PDO::FETCH_ASSOC);
    $level3downlines = $r3['member'];
    // Do actions specific to 1st row before the loop
    echo "make orange juice";
    
    // Then fetch the remaining rows with a loop
    // and perform the their actions in the loop.
    // The rowset record pointer has advanced beyond the 1st row
    while($r3 = $q3->fetch(PDO::FETCH_ASSOC)){
      $level3downlines = $r3['member'];
      echo "make all juice";
    }
    

    In practice, I don't often do operations while fetching rows unless the rowset is very large. Instead I would more likely retrieve all rows as an array where I can more easily perform array operations on the set.

    // Get all rows
    $all_rows = $q3->fetchAll(PDO::FETCH_ASSOC);
    
    // Pull the first row off the set
    $first_row = array_shift($all_rows);
    // do whatever with $first_row
    
    // Loop over the other rows
    foreach ($all_rows as $index => $row) {
       // Do whatever with $row
    }
    

    Instead of using array_shift() in that way, when using $index => $row in the foreach loop, you could also check $index == 0 to operate on your first row. You may find this more understandable.

    foreach ($q3->fetchAll(PDO::FETCH_ASSOC) as $index => $row) {
       // First row
       if ($index == 0) {
         // Do first row actions
       }
       else {
         // Do common actions for remaining rows.
       }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器