i have a table of questions, each question have a level for it's hardness from 1 to 4, i want to get number of rows of question of same level.
this is what i tried:
$result1 = mysqli_query($con,"SELECT * from questions WHERE level=1");
$result2 = mysqli_query($con,"SELECT * from questions WHERE level=2");
$result3 = mysqli_query($con,"SELECT * from questions WHERE level=3");
$result4 = mysqli_query($con,"SELECT * from questions WHERE level=4");
$n1 = mysqli_num_rows($result1);
$n1 = mysqli_num_rows($result2);
$n1 = mysqli_num_rows($result3);
$n1 = mysqli_num_rows($result4);
well, it's so bad and use four queries to do that.
another way in my mind was to use php :
$level = array(0,0,0,0);
$result = mysqli_query($con,"SELECT * from questions");
while($res = mysqli_fetch_array($result)){
switch ($res['level']){
case '1':
$level[0]++;
break;
case '2':
$level[1]++;
break;
case '3':
$level[2]++;
break;
case '4':
$level[3]++;
break;
default:
echo "WTF";
}
}
echo "LEVEL 1 :$level[0]<br>LEVEL 2 :$level[1]<br>LEVEL 3 :$level[2]<br>LEVEL 4 :$level[3]<br>";
this use php and not mysql, but looks faster. is there any other FASTER way to achieve this? what's the best way?