Home >Database >Mysql Tutorial >How to Count All Rows in a MySQL Table Using PHP?

How to Count All Rows in a MySQL Table Using PHP?

Linda Hamilton
Linda HamiltonOriginal
2024-11-25 19:52:11845browse

How to Count All Rows in a MySQL Table Using PHP?

MySQL: Count Total Number of Rows in a Table

PHP

The best MySQL command to count the total number of rows in a table without any conditions applied to it is SELECT COUNT(1) FROM table. This query returns a single row with a single column, which contains the total number of rows in the table.

Here's an example of how to use this query in PHP:

<?php
$con = mysql_connect("server.com", "user", "pswd");
if (!$con) {
    die('Could not connect: ' . mysql_error());
}

mysql_select_db("db", $con);

$result = mysql_query("SELECT COUNT(1) FROM table");
$row = mysql_fetch_array($result);

$total = $row[0];
echo "Total rows: " . $total;

mysql_close($con);
?>

This code will output the total number of rows in the table table in the db database.

The above is the detailed content of How to Count All Rows in a MySQL Table 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