Count elements in a database based on how many unique values there are in a field with MySQL


I had this problem come up when I needed to know how many times a particular essay question had been answered with a written submission.

 

$query = "SELECT COUNT(id) FROM table WHERE essay_id = '$essay_id'";

$data = mysqli_query($dbc, $query);

$row = mysqli_fetch_array($data);

echo $row['0'].' times';  // # times

,

Leave a Reply

Your email address will not be published. Required fields are marked *