开发者

my sql command within codeigniter is not working

I tested this function but it doesn't work and no query result produced. What's wrong Im newbie. Is there any AR alternatives? Im trying to create a mini search engine. Thanks so much in advance.

开发者_如何学编程function search($terms)
{
  $sql = "SELECT * FROM products 
          WHERE name LIKE '%$terms%' 
            OR image LIKE '%$terms%' 
            OR code LIKE '%$terms'");
    $query = $this->db->query($sql, array($terms, $terms));
    return $query->result();
}


To bind your query with the data passed to the function, you have to do the following:

$sql = "SELECT * FROM products 
          WHERE name LIKE ? 
            OR image LIKE ?
            OR code LIKE ?";

$this->db->query($sql, array($terms,$terms,$terms));

Also in our posted code, remove the extra parenthesis from string $sql and you are binding only two values, when the query requires 3

Try this:

$sql = "SELECT * FROM products 
          WHERE name LIKE ? 
            OR image LIKE ?
            OR code LIKE ?";
$this->db->like('name', $terms);
$this->db->like('image', $terms);
$this->db->like('code', $terms);
$query=$this->db->get();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜