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!