When should you use count() and when should you use mysql_num_rows() in PHP to count results from a query?
When you are counting the number of rows returned from a query in PHP, it is recommended to use count() when working with arrays or objects, and mysql_num_rows() when working with MySQL result sets. Count() is a more versatile function that can be used with any array or object, while mysql_num_rows() specifically counts the number of rows in a result set from a MySQL query.
// Using count() with arrays or objects
$result = $db->query("SELECT * FROM table");
$rows = $result->fetch_all(MYSQLI_ASSOC);
$num_rows = count($rows);
echo "Number of rows: " . $num_rows;
// Using mysql_num_rows() with MySQL result sets
$result = $db->query("SELECT * FROM table");
$num_rows = mysql_num_rows($result);
echo "Number of rows: " . $num_rows;