Current Location: Home> Function Categories> mysqli_result::$num_rows

mysqli_result::$num_rows

(mysqli_num_rows) Get the number of rows in the result
Name:mysqli_result::$num_rows
Category:MySQLi
Programming Language:php
One-line Description:Get the number of rows in the result set

Function name: mysqli_result::$num_rows()

Applicable version: PHP 5, PHP 7

Usage: the mysqli_result::$num_rows() function is used to get the number of rows in the result set.

Syntax: int mysqli_result::num_rows ( void )

Parameters: This function does not accept any parameters.

Return value: Returns the number of rows in the result set.

Example:

 // 创建数据库连接$servername = "localhost"; $username = "root"; $password = "password"; $dbname = "myDB"; $conn = new mysqli($servername, $username, $password, $dbname); // 检查连接是否成功if ($conn->connect_error) { die("连接失败: " . $conn->connect_error); } // 执行查询$sql = "SELECT * FROM customers"; $result = $conn->query($sql); // 检查是否有结果if ($result->num_rows > 0) { // 输出每一行数据while($row = $result->fetch_assoc()) { echo "ID: " . $row["id"]. " - Name: " . $row["name"]. " - Email: " . $row["email"]. "<br>"; } } else { echo "0 结果"; } // 关闭连接$conn->close();

In the example above, we first create a database connection and then execute a query statement. Use $result->num_rows to get the number of rows in the result set. If the number of rows is greater than 0, then loop through each row of data through $result->fetch_assoc() and output. If the number of rows is 0, the output is "0 result". Finally, we closed the database connection.

Make sure you have executed the query and got the result set before using $result->num_rows .

Similar Functions
Popular Articles