Prepared Statements # of rows

Prepared Statements # of rows

am 04.03.2011 13:30:51 von ron.piggott

------=_NextPart_000_0064_01CBDA3E.16CD6C60
Content-Type: text/plain;
charset="utf-8"
Content-Transfer-Encoding: quoted-printable


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
â€=9CEncouragement from Godâ€=99s Wordâ€=9D
http://www.TheVerseOfTheDay.info =20

------=_NextPart_000_0064_01CBDA3E.16CD6C60--

Re: Prepared Statements # of rows

am 04.03.2011 13:55:32 von Adriano Rodrigo Guerreiro Laranjeira

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" 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


--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php