pdo how to check if it is 1st record that retrieve from database?

China☆狼群 提交于 2020-01-11 13:23:16

问题


              $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?


回答1:


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



回答2:


I'm not sure if I understood you correctly, but you want to do a special action for the first row,

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

UPDATE: Michael Berkowski's answer is better and faster.



来源:https://stackoverflow.com/questions/46507293/pdo-how-to-check-if-it-is-1st-record-that-retrieve-from-database

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!