What are some common functions in PHP that can be used to count distinct values in a MySQL table?

To count distinct values in a MySQL table using PHP, you can use the COUNT() function along with the DISTINCT keyword in your SQL query. This will return the number of unique values in a specific column of the table. You can then fetch this count result in your PHP script to display or use it as needed.

<?php
// Connect to MySQL database
$mysqli = new mysqli("localhost", "username", "password", "database");

// Check connection
if ($mysqli->connect_error) {
    die("Connection failed: " . $mysqli->connect_error);
}

// SQL query to count distinct values in a specific column
$sql = "SELECT COUNT(DISTINCT column_name) AS count FROM table_name";

// Execute the query
$result = $mysqli->query($sql);

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

// Display the count of distinct values
echo "Count of distinct values: " . $count;

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