Current Location: Home> Function Categories> mysql_num_rows

mysql_num_rows

Get the number of rows in the result set.
Name:mysql_num_rows
Category:Uncategorized
Programming Language:php
One-line Description:Get the number of rows in the result set

Function name: mysql_num_rows()

Applicable version: PHP 5.0.0 - PHP 7.0.0 (deprecated)

Usage: The mysql_num_rows() function is used to get the number of rows in the result set.

Syntax: int mysql_num_rows ( resource $result )

parameter:

  • result: A MySQL result resource, usually the result returned after executing a query statement using the mysql_query() function.

Return value: Returns the number of rows in the result set, and returns 0 if the result set is empty or error.

Example:

 // 连接到MySQL 数据库$link = mysql_connect("localhost", "username", "password"); if (!$link) { die('连接数据库失败: ' . mysql_error()); } // 选择数据库$db_selected = mysql_select_db("database_name", $link); if (!$db_selected) { die ("无法使用指定的数据库: " . mysql_error()); } // 执行查询语句$result = mysql_query("SELECT * FROM table_name", $link); if (!$result) { die('查询失败: ' . mysql_error()); } // 获取结果集中的行数$num_rows = mysql_num_rows($result); echo "结果集中的行数为:" . $num_rows; // 释放结果集mysql_free_result($result); // 关闭数据库连接mysql_close($link);

Notes:

  1. The mysql_num_rows() function has been deprecated and is not recommended for use in new projects. It is recommended to use mysqli or PDO extensions to operate the database.
  2. Before using the mysql_num_rows() function, you need to connect to the database and select the database to operate on.
  3. After executing the query statement, return the result set through the mysql_query() function, and then use the mysql_num_rows() function to obtain the number of rows in the result set.
  4. Finally, remember to free the result set and close the database connection to free up resources and avoid security issues.
Similar Functions
Popular Articles