mysql_num_rows function

 The PHP mysql_num_rows function is used to count the number of rows in the MySQL SELECT result set. When PHP retrieves several pieces of data from the MysQL database, we don’t know how many such so-called pieces of data are. At this time, you can use PHP Built-in mysql_num_rows function for statistics, the execution efficiency is quite good.


Basic syntax of PHP mysql_num_rows function
int mysql_num_rows ( resource $result )
The resource $result in the syntax is the result set of mysql select. mysql_num_rows will return a number after counting all the rows. It should be noted that this function is only valid for the result set of the SELECT syntax. For the number of rows in the result set of INSERT, UPDATE, DELETE, etc., please use the mysql_affected_rows function, but mysql_affected_rows was discarded after MySQL 5.5.0 and must be replaced by mysqli_affected_rows or PDOStatement::rowCount.

PHP mysql_num_rows function example
<?php
$TestLink = mysql_connect("localhost", "UserName", "Passsword");
mysql_select_db("SiteDB", $TestLink);

$Result = mysql_query("SELECT * FROM table", $TestLink);
$num_rows = mysql_num_rows($Result);

echo "總共有 $num_rows 筆資料";
?>
Example output
There are a total of data
The example first uses mysql_connect to open a database connection, and then uses mysql_select_db to search a database. The mysql_query function is used to execute a SQL search syntax, and then the key mysql_num_rows function of this article can be used to determine how many data have been searched. This is a very basic way of judging search results.

Post a Comment

0 Comments