I currently do two queries when using prepared statements. One to fetch the results and another to fetch the number of found rows. I could previously do this with one query with mysql_num_rows. With larger queries i don’t want to have to copy and paste the query and use COUNT every query. Is there a way to do this in one query like with mysql_num_rows?
$connectdb->prepare("SELECT * FROM users WHERE username=:username");
$connectdb->prepare("SELECT COUNT(*) FROM users WHERE username=:username");
$query = $connectdb->execute(array(':username'=>$username));
$numrows = $query->fetchColumn();
if($numrows!=0) {
while(false !==($row = $query->fetch()))
{
The statement already has the row count:
PDOStatement::rowCount()So use
$query->rowCount();and your first query onlyPS: your code doesn’t look like it even would work. The correct one should be: