0

I need to display the same query multiple time but without array because i need different data

example

$re = $bdd->query("SELECT url, name FROM type);
while($do = $re->fetch()) {
    echo $do['url'];
}
while($do = $re->fetch()) {
    echo $do['name'];
}

What is the best solution ? Thanks

Joe e
  • 33
  • 2
  • possible duplicate of [PDO fetchAll group key-value pairs into assoc array](http://stackoverflow.com/q/7451126) – mario Jul 02 '15 at 20:59
  • Why do you insist on doing it without an array? Of course you should use an array! Just fetch both columns beforehand, then use your two distinct loops to output one or the other. – mario Jul 02 '15 at 21:01
  • One array for each ? but why `$re->fetch()` work only for the first ? – Joe e Jul 02 '15 at 21:07

1 Answers1

1

Don't use ->fetch twice. Get a complete list with ->fetchAll.

$re = $bdd->query("SELECT url, name FROM type");
$result_list = $re->fetchAll();
// …
foreach ($result_list as $do) {
    echo $do['url'];
}
// …
foreach ($result_list as $do) {
    echo $do['name'];
}

This is much easier. It simply utilizes the resulting array twice. Which is why there's no point in issueing the database query twice.

mario
  • 144,265
  • 20
  • 237
  • 291