Home  >  Article  >  Backend Development  >  How to get the number of records in database query results using PHP

How to get the number of records in database query results using PHP

PHPz
PHPzOriginal
2023-03-31 09:10:241073browse

PHP is a popular server-side scripting language that is widely used in website development. When developing a website, it is often necessary to query the data in the database and obtain the number of records. This article will introduce how to use PHP to get the number of records in the database query results.

First you need to connect to the database. This can be achieved using the MySQLi and PDO extension libraries in PHP.

The following is a sample code for using the MySQLi extension library to connect to the MySQL database:

$servername = "localhost"; //数据库主机名
$username = "username"; //用户名
$password = "password"; //密码
$dbname = "myDB"; //数据库名

// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);

// 检查连接是否成功
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
}

If you want to use the PDO extension library to connect to the database, you can use the following code:

$servername = "localhost"; //数据库主机名
$username = "username"; //用户名
$password = "password"; //密码
$dbname = "myDB"; //数据库名

// 创建连接
try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    // 设置 PDO 错误模式为异常
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "连接成功";
}
catch(PDOException $e)
{
    echo "连接失败: " . $e->getMessage();
}

Connect to the database After success, the query statement can be executed. The following is a simple query example:

$sql = "SELECT * FROM mytable";
$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 结果";
}

In this code, a SELECT statement is first executed to query all data in the mytable table. Use the $conn->query() method to execute the statement and assign the result to the $result variable. Then determine whether the data has been queried by judging whether the value of the $result->num_rows attribute is greater than 0. If the data is queried, use a while loop to traverse the query results and output each record.

If you only need to get the number of records in the query results, you can use the following code:

$sql = "SELECT COUNT(*) AS count FROM mytable";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // 输出数据
    while($row = $result->fetch_assoc()) {
        echo "记录数量:" . $row["count"];
    }
} else {
    echo "0 结果";
}

In this code, use the SELECT COUNT(*) statement to count the number of records in the mytable table. . Also use the $conn->query() method to execute the query statement and assign the result to the $result variable. Then iterate through the query results through a while loop to output the number of records.

If you use the PDO extension library to connect to the database, you can use the following code to obtain the number of records in the query results:

$sql = "SELECT COUNT(*) AS count FROM mytable";
$result = $conn->query($sql);

if ($result->rowCount() > 0) {
    // 输出数据
    while($row = $result->fetch(PDO::FETCH_ASSOC)) {
        echo "记录数量:" . $row["count"];
    }
} else {
    echo "0 结果";
}

This code is similar to the MySQLi example above, using the SELECT COUNT(*) statement to count the number of records. Use the $conn->query() method to execute the query statement and assign the result to the $result variable. Then iterate through the query results through a while loop to output the number of records. It should be noted here that the method to obtain the number of records in the PDO extension library is $row->rowCount().

Summary: When using PHP to write code to query the database, you need to connect to the database and execute the query statement to obtain the results. To get the number of records in the query results, you can use the above sample code. Use the $num_rows attribute in the MySQLi extension library to get the number of records, and use $row->rowCount() in the PDO extension library.

The above is the detailed content of How to get the number of records in database query results using 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