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条)

报告相同问题?

悬赏问题

  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000
  • ¥30 ppOCRLabel导出识别结果失败