Welcome Guest, Not a member yet? Register   Sign In
Cannot wrap query condition between brackets
#1

I'm trying to check if a post title or content is equal to a specific string, and this post must have a specific type, so I wrote this query:

PHP Code:
$builder $this->post
            
->select('blog_posts.*, users.username as author')
            ->join('users''blog_posts.author_id = users.id''left')
            ->where('type'$type)
            ->like('title'$search)
            ->orLike('content'$search)
            ->orderBy($order$sort); 

this will generate the following result:

PHP Code:
SELECT `blog_posts`.*, `users`.`username` as `author`
FROM `blog_posts`
LEFT JOIN `usersON `blog_posts`.`author_id` = `users`.`id`
WHERE `titleLIKE '%%' ESCAPE '!'
OR  `contentLIKE '%%' ESCAPE '!'
AND `type` = 'post'
ORDER BY `titleASC
 LIMIT 6 

and return this:

[Image: Ti7SlSg.jpg]

but the correct query is this:

PHP Code:
SELECT `blog_posts`.*, `users`.`username` as `author`
FROM `blog_posts`
LEFT JOIN `usersON `blog_posts`.`author_id` = `users`.`id`
WHERE (`titleLIKE '%%' ESCAPE '!'
OR  `contentLIKE '%%' ESCAPE '!')
AND `
type` = 'post'
ORDER BY `titleASC
 LIMIT 6 

'cause I need this output:

[Image: VquDMd8.jpg]

is there a way to make this query using the CodeIgniter query builder?
Reply
#2

Yes, use Query Grouping - https://codeigniter.com/user_guide/datab...y-grouping
Reply




Theme © iAndrew 2016 - Forum software by © MyBB