Home > front end >  Using a Variable in WHERE clause does not work
Using a Variable in WHERE clause does not work

Time:07-19

I'm running a simple query that is working, that is, without a PHP variable in the WHERE clause.

However, when I insert the variable, it does nothing.

<?php 
   $query = $this->db->query("SELECT `rem1` FROM `exam_group_exam_results` WHERE `exam_group_class_batch_exam_student_id`= $student_value->id and `exam_group_class_batch_exam_subject_id`=1");
         $getrem = $query->row();
     echo $getrem->rem1;?>

But when I insert just a value, everything works

<?php 
   $query = $this->db->query("SELECT `rem1` FROM `exam_group_exam_results` WHERE `exam_group_class_batch_exam_student_id`= 11 and `exam_group_class_batch_exam_subject_id`=1");
         $getrem = $query->row();
     var_dump($getrem);?>

I var_dump this variable student_value['id'] and it printed out the correct value which is 11.

I've been at this for hours. Please help

CodePudding user response:

It's probably a syntax problem

   $query = $this->db->query("SELECT `rem1` FROM `exam_group_exam_results` WHERE `exam_group_class_batch_exam_student_id`= ".$student_value->id." and `exam_group_class_batch_exam_subject_id`=1");

Otherwise, check whether your variable is an object or an array

CodePudding user response:

Try to assign the value of your object to another variable. Example

$id_value = student_value['id'];

Then use the variable in your SQL code

$query = $this->db->query("SELECT `rem1` FROM `exam_group_exam_results` WHERE `exam_group_class_batch_exam_student_id`= $id_value and `exam_group_class_batch_exam_subject_id`=1");
     
  •  Tags:  
  • php
  • Related