What function in MySQL can be used to count rows based on a specific condition?
To count rows based on a specific condition in MySQL, you can use the `COUNT` function along with a `WHERE` clause to specify the condition. This allows you to retrieve the number of rows that meet the specified criteria in a table.
// Connect to MySQL database
$mysqli = new mysqli("localhost", "username", "password", "database");
// Check connection
if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}
// Query to count rows based on a specific condition
$result = $mysqli->query("SELECT COUNT(*) FROM table_name WHERE condition");
// Fetch result
$row = $result->fetch_row();
// Output the count
echo "Number of rows based on condition: " . $row[0];
// Close connection
$mysqli->close();