-1

Title pretty much says it all. If fetching is the only way to test, is there a way to reset the fetch iterator?

user116032
  • 321
  • 2
  • 15
  • Typically you would do a `SELECT COUNT(*) FROM ...` query if you just wanted a count. If you were going to use all the data anyways, you could also fetchAll to get all rows and get a count() of those. You can then use that returned array later in your code when you normally would just have used fetch. You might try the PDOStatement::rowCount method, but you can't really guarantee that it will return the row count for select statements as told by the manual. In some cases it might work. – Jonathan Kuhn Feb 02 '17 at 22:54
  • Not a dup. Asked about iterator. I came up with this, after an initial fetch to test for any success. while($pending_fetch || ($row = $sth->fetch ....)) { $pending_fetch = 0 – user116032 Feb 02 '17 at 22:56
  • Riggs please include link to what you think was dup. I didn't find it. I'm ready to pull the plug if there is one. I read the one you listed. It didn't consider iterator manipulation. – user116032 Feb 02 '17 at 22:59
  • 1
    The duplicate answer suggests exactly what you selected as the correct answer here which basically states to use a `select count(*)...` statement beforehand to get a count. – Jonathan Kuhn Feb 02 '17 at 23:15

1 Answers1

-1

Yes, well kinda, you can perform it inline.

if (!$pdo->query("SELECT COUNT(*) FROM mytable")->fetchColumn()) {
    echo "Ooops, didn't find anything";
}
jpschroeder
  • 6,636
  • 2
  • 34
  • 34