Syn Syn - 4 months ago 9
MySQL Question

PHP if mysqli query statement

I am in the process of creating a website that displays a group of questions, however my problem is checking if the user has answered the question or not, and if they have, display an “Answered” label.

However the currently, It is displayed the “Answered” Label for each and every question, even if the answer is not in the submissions table. Any help would be appreciated.

while($data = mysqli_fetch_row($result)){
if($data[0] != null){
echo('
<div class="col-md-4 col-sm-5">
<div class="panel panel-default text-center">
<div class="panel-heading">
<span class="fa-stack fa-5x">
<i class="fa fa-circle fa-stack-2x text-default"></i>
<i class="fa fa-codepen fa-stack-1x fa-inverse"></i>
</span>
</div>
<div class="panel-body">
');
if($result4 = mysqli_query($mysqli,"SELECT * FROM submissions where teamID='$teamName' and questionID='$data[0]' and status='correct'")){
echo "Answered";
} else{
echo "Not Answered";
}

echo ('
<h4>'.$data[6].' - <small><i>'.$data[8].' points</i></small></h4>
<p>'.$data[7].'</p>
<a href="question.php?id='.$data[0].'" class="btn btn-primary btn-block">View Question </a>
</div>
</div>
</div>
');} else{
echo "No More Questions";
}
}


I know I am making a basic or stupid error here, So any help appreciated.

Answer
//Mysql query to find number of answers for particular question.
$answercount = mysqli_query($mysqli, "SELECT * FROM submissions where teamID='$teamName' and questionID='$data[0]' and status='correct'");
$answercount = mysqli_num_rows($answercount);

//PHP code
if($answercount>0){
    echo "Answered";
} else{
    echo "Not Answered";
}