Php 使查询不相等
我有Codeigniter应用程序,我有条件从[user]表中查找用户(id),如果等于[courses]表中的(用户),我想将“=>”反转为不相等 此代码:Php 使查询不相等,php,codeigniter,Php,Codeigniter,我有Codeigniter应用程序,我有条件从[user]表中查找用户(id),如果等于[courses]表中的(用户),我想将“=>”反转为不相等 此代码: foreach ($courses as $c){ $where= array('id'=>$c->student); 你能不能再找一个地方 foreach ($courses as $c){ foreach ($
foreach ($courses as $c){
$where= array('id'=>$c->student);
你能不能再找一个地方
foreach ($courses as $c){
foreach ($c as $key => $value){
if($value != 'student'){$dosomething;}
}
}
嗯,我不明白你的期望是什么,但是在这里 如果您想从codeigniter使用db查询生成器进行查询,并尝试定义“不相等”,只需添加
=代码>
所以,试着替换这个
$where=array('id'=>$c->student)代码>
为此:
$where=array('id!='=>$c->student)代码>
或者这是我的建议:
$where=('id!=',$c->student)代码>
之后,您可以使用以下生成器:
$this->db->select('*');
$this->db->from('user u');
$this->db->join('courses c', 'c.user_id = u.id');
$this->db->where($where);
$query = $this->db->get();
return $query->result();
然后是完整代码:
foreach ($courses as $c){
$where= ('id !=', $c->student);
$this->db->select('*');
$this->db->from('user u');
$this->db->join('courses c', 'c.user_id = u.id');
$this->db->where($where);
// or you can try direct subtitute $this->db->where('id !=', $c->student);
$query = $this->db->get();
$result = $query->result();
foreach($result => $res){
echo $res->name; # OR YOUR EXPECTED COLUMN
}
}
你能更好地解释你的问题吗?!在我看来,这个问题根本不像是CodeIgniter查询。你研究过吗?这个问题太广泛/研究不足。也就是说,这个孤立的问题是重复的,因此最好是关闭而不是回答,这样堆栈溢出就不必存储冗余内容。进行迭代查询不是一个好建议。我将链接这些查询生成器方法。select()
不是必需的。您正在选择全部,*'。您的$where
语法有缺陷。