How can MySQL data be counted and output using PHP?

To count and output MySQL data using PHP, you can execute a SQL query to count the rows in a table and then fetch the result using PHP. You can use the mysqli extension in PHP to connect to the MySQL database, execute the query, fetch the result, and display the count.

<?php
// 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 in a table
$query = "SELECT COUNT(*) as total FROM table_name";
$result = $mysqli->query($query);

// Fetch the result
$row = $result->fetch_assoc();
$count = $row['total'];

// Output the count
echo "Total rows in table: " . $count;

// Close connection
$mysqli->close();
?>