Re: Prepared Statements # of rows
| From: | Adriano Rodrigo Guerreiro Laranjeira | Date: | Fri, 04 Mar 2011 12:55:32 +0000 |
| Subject: | Re: Prepared Statements # of rows | ||
| References: | 1 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-47708@lists.php.net to get a copy of this message | ||
Hey friend!
You can use the PDOStatement::RowCount, but there is a problem (extracted from the PHP Manual, http://php.net/manual/en/pdostatement.rowcount.php):
PDOStatement->rowCount - Returns the number of rows affected by the last SQL statement. If the last SQL statement executed by the associated PDOStatement was a SELECT statement, some databases may return the number of rows returned by that statement. However, this behaviour is not guaranteed for all databases and should not be relied on for portable applications.
You should test if the expected behavior happens with this method, or use the method below (less elegant):
$sql = "SELECT COUNT(*) FROM fruit WHERE calories > 100";
if ($res = $conn->query($sql)) {
/* Check the number of rows that match the SELECT statement */if ($res->fetchColumn() > 0) {
/* Issue the real SELECT statement and work with the results */
$sql = "SELECT name FROM fruit WHERE calories > 100";
foreach ($conn->query($sql) as $row) {
print "Name: " . $row['NAME'] . "\n";
}
}
/* No rows matched -- do something else */
else {
print "No rows matched the query.";
}
}
Best regards,
Adriano Laranjeira.
On Fri, 4 Mar 2011 07:30:51 -0500 "Ron Piggott" <ron.piggott@actsministries.org> wrote: When I used Prepared Statements how do I check for the # of rows found (Equal to mysql_numrows )? IE Following the command: $stmt->execute() or die(print_r($stmt->errorInfo(), true)); Ron The Verse of the Day “Encouragement from God’s Word” http://www.TheVerseOfTheDay.info