Using mysql_num_rows()

The next logical function to discuss is mysql_num_rows(). This function returns the number of rows retrieved by a SELECT query, taking the query result as a parameter.

I’ll use this function in two different ways. First I’ll modify view_users.php to list the total number of registered users. Second, I’ll modify register.php to test if a username has already been taken.

To modify view_users.php:

1.
Open view_users.php (refer to Script 6.7) in your text editor.
2.
Before the if ($result) conditional (line 12), add this line (Script 6.9)
$num = mysql_num_rows ($result);
This line will assign the number of returned rows to the $num variable.
3.
Change the original $result conditional to if ($num > 0) { The conditional ...

Get PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.