Home  >  Article  >  Backend Development  >  Let’s talk about the various ways to query the number of database rows in PHP

Let’s talk about the various ways to query the number of database rows in PHP

PHPz
PHPzOriginal
2023-04-11 15:08:231453browse

With the development of the Internet era, database applications are becoming more and more widespread, and the demand for the number of database query rows is also increasing. In the PHP language, querying the number of database rows is a relatively basic operation. There are many ways to implement this function.

1. Use the mysql_num_rows() function to query the number of rows

The mysql_num_rows() function is a function used to obtain the number of rows in the query result in the built-in mysql extension in PHP. It has the following syntax:

int mysql_num_rows (resource $result)

$result refers to the resource handle of the result of the query, and the returned result is an integer, indicating the total number of rows in the query result. The following code demonstrates how to use the mysql_num_rows() function through a MySQL query in PHP:

// 定义用于连接数据库的信息等
$host = "localhost";
$user = "root";
$pass = "123456";
$db   = "mydatabase";

// 连接到MySQL数据库
$link = mysql_connect($host, $user, $pass);

// 选择要操作的数据库
mysql_select_db($db, $link);

// 执行MySQL查询操作
$query = "SELECT * FROM mytable";      // SQL语句
$result = mysql_query($query, $link);  // 执行SQL查询

// 使用mysql_num_rows()查询结果的行数
$num_rows = mysql_num_rows($result);
echo "总行数为:" . $num_rows;

?>

In the above code, First connect to the MySQL database through mysql_connect(), then select the database to be operated through mysql_select_db(), then use mysql_query() to perform the SQL query operation, and finally use the mysql_num_rows() function to query the number of rows in the result.

2. Use the PDO class to query the number of rows

PDO is a commonly used database extension class in the PHP language. It can connect to many different types of databases such as MySQL, PostgreSQL, Oracle, and Microsoft SQL Server. database, and can easily query the number of rows in the result through the rowCount() method of the PDOStatement class. The PDO code demonstration is as follows:

// 定义用于连接数据库的信息等
$host = "localhost";
$user = "root";
$pass = "123456";
$db   = "mydatabase";

// 连接到MySQL数据库
try {
    $dbh = new PDO("mysql:host={$host};dbname={$db}", $user, $pass);
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}

// 执行MySQL查询操作
$query = "SELECT * FROM mytable";      // SQL语句
$stmt = $dbh->prepare($query);         // 准备SQL语句,获取PDOStatement对象
$stmt->execute();                      // 执行SQL查询

// 查询结果行数
$num_rows = $stmt->rowCount();
echo "总行数为:" . $num_rows;</p>
<p>?></p> <p>3. Use the mysqli_num_rows() function to query the number of rows</p>
<p>Mysqli is an extension class used in PHP to connect to the MySQL database. It supports object-oriented programming and process-oriented programming. It is very similar to mysql syntax. Similar, the mysqli_num_rows() function is used to obtain the number of rows in the query result set. The usage method is similar to the mysql_num_rows() function. The code is as follows: </p>
<p><?php</p><pre class="brush:php;toolbar:false">// 定义用于连接数据库的信息等
$host = "localhost";
$user = "root";
$pass = "123456";
$db   = "mydatabase";

// 连接到MySQL数据库
$link = mysqli_connect($host, $user, $pass, $db);

// 执行MySQL查询操作
$query = "SELECT * FROM mytable";      // SQL语句
$result = mysqli_query($link, $query);// 执行SQL查询

// 使用mysqli_num_rows()查询结果的行数
$num_rows = mysqli_num_rows($result);
echo &#39;总行数为:&#39; . $num_rows;

?>

The above are three methods of using PHP to query the number of database rows. Readers can choose the appropriate method according to their own needs.

The above is the detailed content of Let’s talk about the various ways to query the number of database rows in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn