简体   繁体   中英

Count the amount of values in a Mysql table with php

I made a school voting with 10 questions. There you can check one teacher for every question (in total there are 80 teachers). When someone has voted for all questions, a new row in my mysql table will be inserted.

My Mysql Table has 10 fields (for every question one(q1,q2,q3)). The name of the teacher for one question will be an integer (every teachers has an id from 1 - 82).

That is what the table looks like

Now I want to find out for which teacher is how often voted for each question. That should do a php script. It should echo something like this:

The result should be something like this

How to do this?

This query will give number of times each teacher is voted for question q1.

$sql = "SELECT q1, COUNT(*) AS votes FROM table1 GROUP BY q1 ORDER BY votes";
$ref = $result->query($sql);
while($row = mysqli_fetch_assoc($ref))
{
  echo "$row[votes] times $row[q1]</br>";

}

You can do the same for other questions, just by changing the column name for question number

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM