Reputation: 87
How can I easily get the number of rows returned with "SELECT *
" SQL-query?
function valid_credentials($db,$name,$pw) {
try {
$sth = $db->prepare("SELECT * FROM ib_members WHERE name=:val AND pw=:val2");
$sth->bindValue(":val",$name);
$sth->bindValue(":val2",$pw);
$sth->execute();
$numrows = $sth->fetchColumn();
return $numrows;
} catch (PDOException $e) {
return $e->getMessage();
}
}
That returns 14, which is definately not the number of rows returned, but it's the ID that the first row has.
Upvotes: 0
Views: 1662
Reputation: 14154
For DELETE
, INSERT
, or UPDATE
statements, use PDOStatement::rowCount().
For SELECT
statements, count the rows manually in your while( $sth->fetch() )
loop (you can break
out of the loop if the count exceeds a certain threshold, for instance), or execute a separate query to have the database return the row count (for example, SELECT COUNT(*) FROM table WHERE column = ?
).
Upvotes: 1