Counting Rows in MySQL with PHP
The SELECT COUNT(*) function is commonly used to count rows in MySQL. To use it through PHP, follow these steps:
PHP Example:
<code class="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(*) FROM table_name"); $row = mysql_fetch_array($result); $total = $row[0]; echo "Total rows: " . $total; mysql_close($con); ?></code>
This code establishes a connection, selects the database, executes the COUNT(*) query, fetches the result, stores the total in the $total variable, and finally prints it out.
The above is the detailed content of How to Count Rows in MySQL with PHP Using SELECT COUNT(*). For more information, please follow other related articles on the PHP Chinese website!