mysql_num_rows() function in PHP

Topic

What is mysql_num_rows() function in PHP?
How does mysql_num_rows() works?

Explanation

This mysql_num_rows function in php returns number of rows in result.
Syntax
int mysql_num_rows ( resource $result )

Returns number of rows in result on success, or NULL on error.
mysql_num_rows () function retrieves the number of rows from a result set. This command is only valid for statements like SELECT or SHOW that return an actual result set. To retrieve the number of rows affected by a INSERT, UPDATE, REPLACE or DELETE query, use mysql_affected_rows().
Example

<?php
//Attempt to connect to the default database server
$link = mysql_connect("mysql_host", "mysql_user", "mysql_password")
or die ("Could not connect");
mysql_select_db("database", $link);
$result = mysql_query("SELECT * FROM table1", $link);
//returns number of rows in result
$num_rows = mysql_num_rows($result);
//Print number of rows
echo "$num_rows Rowsn";
mysql_close($link);
?>

RESULT:
10 Rows

See also:
mysql_affected_rows(), mysql_connect(), mysql_data_seek(), mysql_select_db() and mysql_query().

PHP Topics