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

Php pdo如何检查是否是从数据库检索的第一条记录?,php,pdo,Php,Pdo,假设输出是来自数据库“Zac”、“Michelle”、“Andrew”的3条记录。“Zac”是从数据库中获取的第一条记录,如何编写“if”检查记录是否为第一条记录的语句?首先,如果返回的记录顺序很重要,则必须在查询中明确包含一个order BY子句,以指定对结果进行排序的列。否则,返回的行的顺序在技术上是不确定的 要在第一个获取的行上执行与在以后的行上不同的操作,只需首先在循环外部调用fetch(),然后在其余行上循环。每次调用PDOStatement::fetch()都会提升行集记录指针,因此

假设输出是来自数据库“Zac”、“Michelle”、“Andrew”的3条记录。“Zac”是从数据库中获取的第一条记录,如何编写“if”检查记录是否为第一条记录的语句?

首先,如果返回的记录顺序很重要,则必须在查询中明确包含一个
order BY
子句,以指定对结果进行排序的列。否则,返回的行的顺序在技术上是不确定的

要在第一个获取的行上执行与在以后的行上不同的操作,只需首先在循环外部调用
fetch()
,然后在其余行上循环。每次调用
PDOStatement::fetch()
都会提升行集记录指针,因此
while
循环只会在已经检索到的记录之后迭代记录

              $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";
                      }
              }
实际上,除非行集非常大,否则我通常不会在获取行时执行操作。相反,我更可能以数组的形式检索所有行,这样我可以更容易地对集合执行数组操作

// 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";
}
foreach
循环中使用
$index=>$row
时,也可以选中
$index==0
对第一行进行操作,而不是以这种方式使用
array\u shift()
。你可能会发现这更容易理解

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

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

要在第一个获取的行上执行与在以后的行上不同的操作,只需首先在循环外部调用
fetch()
,然后在其余行上循环。每次调用
PDOStatement::fetch()
都会提升行集记录指针,因此
while
循环只会在已经检索到的记录之后迭代记录

              $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";
                      }
              }
实际上,除非行集非常大,否则我通常不会在获取行时执行操作。相反,我更可能以数组的形式检索所有行,这样我可以更容易地对集合执行数组操作

// 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";
}
foreach
循环中使用
$index=>$row
时,也可以选中
$index==0
对第一行进行操作,而不是以这种方式使用
array\u shift()
。你可能会发现这更容易理解

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

我不确定我是否理解正确,但你想在第一排做一个特别的动作

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

更新:Michael Berkowski的答案更好更快。

我不确定我是否理解正确,但你想在第一排做一个特殊动作

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

更新:Michael Berkowski的答案更好更快。

第一个记录基于什么?插入顺序?基于$level3downlines php变量。上面的代码使用while循环,所以$level3downlines变量第一个循环是“Zac”,第二个循环是“Michelle”,第三个循环是“Andrew”。第二个和第三个循环执行相同的操作,只有第一个循环必须执行其他不同的操作,这就是为什么我需要if else语句来检查它是否是while loop.1st记录的第一个循环/1st结果基于什么?插入顺序?基于$level3downlines php变量。上面的代码使用while循环,所以$level3downlines变量第一个循环是“Zac”,第二个循环是“Michelle”,第三个循环是“Andrew”。第二个和第三个循环执行相同的操作,只有第一个循环必须执行其他不同的操作,这就是为什么我需要if-else语句来检查它是否是while循环的第一个循环/第一个结果。您的答案更容易理解,而且非常有创意。我认为你的方法更能优化速度,对吗?因为只取1次,而Michael的方法取2次。实际上我不确定,但你可以做一个测试,看看哪个更快。你的答案更容易理解,而且很有创意。我认为你的方法更能优化速度,对吗?因为Michael的方法只取1次,而Michael的方法取2次。我不确定,但你可以做一个测试,看看哪一个更快。