doulao2916 2012-09-03 20:06
浏览 20

Codeigniter:模型查询不检索任何数据

On my database I have fields that has value of

1 Blue White

2 White Blue

3 Blue

4 White

for every ids.

I have a controller where I declare $color="White Blue" and passed it on the model. On my model I use 'explode' to separate the two words and use a for-each to extract.

Here's my model:

  public function test($color){

    $arry = explode(" ", $color); 

    foreach($arry as $colr){
    $this->db->where("tags like '%$colr%' or 
            tags like '%$colr'or 
            tags like '$colr%' ");
            }


    $q=$this->db->get('test');
    return $q;

    }

When I try to run it on localhost, it gives me Call to a member function result() error. I know that this kind of error means it doesn't retrieve any data from my database. When I tried to put an error on my query, I found out that instead if using 'OR' after the first for-each it uses 'AND'. Do you have any idea on how I can make this work?

  • 写回答

2条回答 默认 最新

  • dq_1984 2012-09-03 20:09
    关注
    tags like '%$colr'or
    

    is missing a space?

    tags like '%$colr' or
    

    Futhermore, putting the $colr between '%' should be sufficient. No need for the other lines.

    So, this should do the trick:

    public function test($color){
    
        $arry = explode(" ", $color); 
    
        foreach($arry as $colr){
          $this->db->where("tags like '%$colr%' "); 
        }
    
        $q=$this->db->get('test');
        return $q;
    
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类