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

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

本文介绍了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";
                      }
              }

假设输出是数据库"Zac","Michelle",Andrew"中的3条记录."Zac"是从数据库中获取的第一条记录,如何编写"if"语句以检查记录是否为第一条记录或不是吗?

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?

推荐答案

首先,如果返回的记录顺序很重要,则必须在查询中显式包括ORDER BY子句,以指定对结果进行排序的列.否则,返回的行的顺序在技术上是不确定的.

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.

要在获取的第一行和后面的行上进行不同的操作,您可以简单地先在循环外部调用fetch(),然后在其余部分循环.每次对PDOStatement::fetch()的调用都将使行集记录指针前进,因此while循环将仅在已检索到记录之后对记录进行迭代.

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
}

不是以这种方式使用array_shift(),而是在foreach循环中使用$index => $row时,也可​​以选中$index == 0在第一行上进行操作.您可能会发现这更容易理解.

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.
   }
}

这篇关于pdo如何检查是否是从数据库检索的第一条记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 11:28