I’ve switched a couple of years ago to PDO to manage all the database actions. PDO (PHP Data Objects) is an object orientated extension that requires at least php 5.1. In many cases software improves and is often more easy to use. But why did the php development-teamĀ forgot to write a similar function like mysql_num_rows?
In this case you always need to run an extra query to retrieve the total records. See the examples below.
Old style
$sql = "SELECT id, name FROM table WHERE cat = 1"; $res = mysql_query($sql); $count = mysql_num_rows($res);
Read more about the basic error handler in a previous article.
PDO style
try{
$sql = "SELECT COUNT(id), name FROM table WHERE cat = 1";
$res = $this->db->prepare($sql);
$res->execute();
//count the rows
$count = $res->fetchColumn();
}catch(PDOException $e){
//Basic error handler
return $e->getMessage();
return $sql;
}





Comments (2)
Chris says:
Thanks!
When I’ve found this article I’ve finished my search!
Normal says:
What a perfect answer to my search, bow man!, you are the best