Home  >  Article  >  Topics  >  How to query the number of mysql records in php

How to query the number of mysql records in php

藏色散人
藏色散人Original
2020-10-19 11:49:273161browse

How to query the number of mysql records in php: 1. Use the "mysql_num_rows" function to query the number of mysql records; 2. Query the number of mysql records by modifying the query statement and using "COUNT(*)" as the query content.

How to query the number of mysql records in php

Recommended: "PHP video tutorial" "php mysql"

PHP query statement ,Return the total number of records

PHP query statement, there are two methods to obtain the total number of records queried.

MySQLi process-oriented approach

$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = mysqli_query($conn, $sql);
 
num_rows=mysqli_num_rows($result);

First, use the mysql_num_rows function, example code:

<?php
$link = mysql_connect("localhost", "mysql_user", "mysql_password");
mysql_select_db("database", $link);
 
$result = mysql_query("SELECT * FROM table1", $link);
$num_rows = mysql_num_rows($result);
echo "$num_rows Rows\n";
?>

The second is to modify the query statement, using COUNT(*) as the query content, example Code:

<?php
$link = mysql_connect("localhost", "mysql_user", "mysql_password");
mysql_select_db("database", $link);
 
$result = mysql_query("SELECT COUNT(*) FROM table1", $link);
list($num_rows) = mysql_fetch_row($result);
echo "$num_rows Rows\n";
?>

The above is the detailed content of How to query the number of mysql records 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