I'm currently making a game to teach myself PHP as I go. I've already progressed quite a bit but I'm stuck now at the statistics where I want to show how many of each player-class there currently is in the game.
Thanks everyone, finally managed to fix it - not in the way as answered but the answer did help me getting to a result!
Which is:
<?php
echo '<b>Current Classes in the Game :</b>' . '<br />';
$result = $db->query("SELECT COUNT(*) FROM users WHERE role='Warrior'");
$row = $result->fetch_row();
echo $row[0] . ' Warriors' . '<br />';
$result = $db->query("SELECT COUNT(*) FROM users WHERE role='Mage'");
$row = $result->fetch_row();
echo $row[0] . ' Mages' . '<br />';
$result = $db->query("SELECT COUNT(*) FROM users WHERE role='Priest'");
$row = $result->fetch_row();
echo $row[0] . ' Priests' . '<br />';
$result = $db->query("SELECT COUNT(*) FROM users WHERE role='Rogue'");
$row = $result->fetch_row();
echo $row[0] . ' Rogues' . '<br />';
?>
You need execute an sql query like this using mysqli :
select
count(*) as nb,
class
from
yourTable
group by class
I think you want to use MySQL (because you are showing up a screenshot of phpmyadmin)
The SQL code would be
SELECT count(`class`), `class` FROM your_table_name;
This code can be run by php, for example by using mysqli
. Your php code would be
$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'my_db');
/* maybe some error handling, visit php website for this */
$query = "SELECT count(`class`), `class` FROM your_table_name;";
$result = $mysqli->query($query)
if($result){
while($row = $result->fetch_row()){
printf("%i: %s
", $row[0], $row[1]);
}
}
(not tested)
In addition you could use GROUP BY
for the SQL code, you could change it to
SELECT count(`class`), `class` FROM your_table_name GROUP BY `class`;
Edit
Make sure to use ` before the "class" in the mysql statement, class is a reserved word in MySQL