Is it possible to check if pdostatement::fetch() has results without iterating through a row?
mysql, pdo, php
Solution
Just put the result of fetch into a variable:
if($row = $q->fetch()) {
// $row contains first fetched row
echo $row['coloumn_name'];
}
else
// no results
Problem
I have a page which needs to check for results, and the way I came up with to do it is successful, but iterates through the first row of results. Is there a way I can check without iterating, or to go back to that first row without executing the query again? I was doing this: ``` $q = pdo::prepare($SQL); $q->execute(array(':foo'=> foo, 'bar'=>bar); if(!q->fetch){ //no results; }else{ //results; }; ``` It does pretty much exactly what I hoped, with the unfortunate side affect of skipping the first row of results. I've resorted to running $q->execute() a second time. Is there a way to avoid doing this?