Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php codeigniter自定义where子句中出错_Php_Mysql_Codeigniter - Fatal编程技术网

Php codeigniter自定义where子句中出错

Php codeigniter自定义where子句中出错,php,mysql,codeigniter,Php,Mysql,Codeigniter,我参考codeigniter用户指南来编写一个自定义where子句。 根据指南上的说明,习惯条款是这样写的 $where = "name='Joe' AND status='boss' OR status='active'"; $this->db->where($where); 但当我在模型浏览器中使用时,会抛出一个错误 A Database Error Occurred Error Number: 1054 Unknown column 'user_name='Joe'

我参考codeigniter用户指南来编写一个自定义where子句。 根据指南上的说明,习惯条款是这样写的

 $where = "name='Joe' AND status='boss' OR status='active'";
 $this->db->where($where); 
但当我在模型浏览器中使用时,会抛出一个错误

A Database Error Occurred

Error Number: 1054

Unknown column 'user_name='Joe'' in 'where clause'

SELECT * FROM (`Management`) WHERE `user_name='Joe'` AND password='boss' OR password='active'

Filename: /var/www/models/hr_login_model.php

Line Number: 28
这只是一个测试查询。我的实际查询是动态的,它也给出了这个错误

$where = "user_name='".$username."' AND password='".$password."' AND Department='".$dep_br."' OR Br_no='".$dep_br."'";

为什么它总是将列名和值都作为列名

前后的用户空间=如下所示

$where = "name = 'Joe' AND status = 'boss' OR status = 'active'"; 
$this->db->where($where); 
使用下列内容

$where = "user_name='$username' AND password='$password' AND (Department='$dep_br' OR Br_no='$dep_br')";
试试这个:

$where = "name='Joe' AND status='boss' OR status='active'";
$this->db->where($where, NULL, FALSE);

这样做会更好更安全

$this->db->where("name","joe");
$this->db->where("status =","boss");
$this->db->or_where("status","active");

这是无关紧要的,空格不是问题(它们甚至不在doc示例中)@Damien Pirsy-它们没有文档记录,但如果您查看第430行的DB_active_rec.php文件,就会发现有一个条件会用“=”检查运算符。这意味着在=operator@Yasitha之前和之后应该有一个空格-一定要让我知道这个解决方案是否对你有帮助。我必须承认我被难住了!没有检查CI的实际代码,但应该指出这是他们文档中的一个bug(如果有人仍在处理)。祝贺曼威尔,说实话,直到我面对这个问题时才检查代码。我费了很大劲,决定检查一下核心文件,然后我找到了它。:)出于好奇:CodeIgniter是如何告诉您如何编写字符串的?它绕过了PHP解析器吗?@lvaroG.Vicario-我试过这个。但它需要在=符号前后加上空格,这样才能工作。谢谢,我认为这是最好的答案。因为在没有()的情况下,查询会给我不需要的行。但是对于()它可以按我所希望的那样工作。但随着Trimantra软件解决方案的推出,空间应该存在@Trimantra软件解决方案不是一个解决方案,但为什么不避免这种不安全的方法(您需要手动转义这些值)并使用一种更安全、更简单的方法?@DamienPirsy-实际上我不知道这是一种不安全的方法。如果你能告诉我一个更好的方法,我会非常感谢。