[PDO]collect data from database
[PDO]collect data from database
I was follow this Guide
to collect Questions from database
after I reach the last question it keep repeat the same last question instead of displaying massage
here is my code
//////// Collected from databse ///////
$question_id=$question_id+1;
$no_questions = $dbo->query("select count(question) from questions
left join servey_question on questions.question_id = servey_question.question_id where servey_id = 'serv01' ")->fetchColumn();
if($question_id > $no_questions){
$next='F'; // Flag is set to display thank you message
}else{
$next='T'; // Flag is set to display next question
$count=$dbo->prepare("select * from questions where question_id=$question_id");
if($count->execute()){
$row = $count->fetch(PDO::FETCH_OBJ);
}
}
$main= array("data"=>array("q1"=>"$row->question","opt1"=>"$row->opt1","opt2"=>"$row->opt2","opt3"=>"$row->opt3","opt4"=>"$row->opt4","opt5"=>"$row->opt5","opt6"=>"$row->opt6","question_id"=>"$question_id","category_id"=>"$row->category_id"),"next"=>"$next"); //Here the error appear
// end of collection from database //////
echo json_encode($main);
in this line the Error appear:
$main= array("data"=>array("q1"=>"$row->question","opt1"=>"$row->opt1","opt2"=>"$row->opt2","opt3"=>"$row->opt3","opt4"=>"$row->opt4","opt5"=>"$row->opt5","opt6"=>"$row->opt6","question_id"=>"$question_id","category_id"=>"$row->category_id"),"next"=>"$next");
display this massage
Notice: Undefined variable: row in C:xampphtdocssurveysurvey0examck.php on line 212
Notice: Trying to get property of non-object in C:xampphtdocssurveysurvey0examck.php on line 212
I try to do this but it's only show me the first question then massage with out move to other questions
$main=array('data'=>array($row),'next'=>$next); //diaplay first question and then thank you massage
so can you please tell me what is goes wrong..please
$row
$row
$main
json_encode()
@Qirel it's stop reporting error but still repeat the last question ans it's suppose to display massage
– lobna
Jun 30 at 10:05
if($count->execute()){ fails, which is why you got the error in the first place. Figure out why by enabling error-reporting, and checking for errors coming from PDO.– Qirel
Jun 30 at 10:15
if($count->execute()){
There is no point using a prepared statement if you aren't parameterizing. That is a misuse of prepared statement.
– user3783243
Jun 30 at 11:18
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
You define
$rowinside a condition, and attempt to access it outside it after - so if the condition fails (as it seemingly does),$rowis never defined. Move$mainand the print ofjson_encode()inside the condition.– Qirel
Jun 30 at 9:51