简体   繁体   中英

Using a Variable in WHERE clause does not work

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

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

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");
     

I think the query syntax is not read the Student ID, so the result will show as is WHERE CLAUSE.

// Try this
$studentId = $student_value->id;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM