Mysql
 sql >> Database >  >> RDS >> Mysql

Come recuperare 2 volte in MYSQL PDO senza FETCHALL

Lo riprendo sembra che tu possa usare i contanti di orientamento del cursore per selezionare il risultato ... codice di esempio in arrivo ... Non l'ho provato, quindi potresti dover giocare un po '. Ciò si basa anche sul presupposto che un PDO::FETCH_ORI_FIRST agisce come un data_seek e lascia il cursore sulla prima posizione invece di riportarlo a quello che era prima.

$stmt = $pdo->prepare('SELECT id FROM table', array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
$stmt->execute();

$first = $pdo->fetch(PDO::FETCH_ASSOC, PDO::FETCH_ORI_FIRST);
$first_row = $first['id'];

// other stuff

// first iteration we rewind to the first record;
$cursor = PDO::FETCH_ORI_FIRST;

while (false !== ($row = $stmt->fetch(PDO::FETCH_ASSOC, $cursor))) {
   $id = $row['id'];
   // successive iterations we hit the "next" record
   $cursor = PDO::FETCH_ORI_NEXT; 
   echo $id;
}

Non penso che tu possa riavvolgere un'istruzione... Supponendo che questi blocchi non siano separati da una serie di ID logici intermedi, fallo semplicemente nel ciclo.

$STH->setFetchMode(PDO::FETCH_COLUMN); // no need to pull an array
$count = 0;
while ($id = $STH->fetch()) {      
  if($count === 0) {
   $first_row = $id;
  }
  echo $id;
  $count++;
}