简体   繁体   English

在 WHERE 子句中使用变量不起作用

[英]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.我正在运行一个正在运行的简单查询,也就是说,WHERE 子句中没有 PHP 变量。

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.我 var_dump 这个变量student_value['id']并打印出正确的值,即 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然后在您的 SQL 代码中使用该变量

$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.我认为查询语法没有读取学生 ID,因此结果将显示为 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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