Dennis Dennis - 1 year ago 141
MySQL Question

Codeigniter Query Builder having clause with IN statement

I am using CodeIgniter query builder. I want to add the having clause in there.
My code looks as follows:
(i omitted the other parts of the query):


And i build the filterarray beforehand like this:

$filterarray = array();
if (!empty($filters['interests'])) {
$interestids = $this->interests_model->getAllIdsByDescription($filters['interests']);
$filterarray['interests IN'] = $interestids;

My function getAllIdsByDescription looks like this:

function getAllIdsByDescription($names){
$res = "(";

$query = $this->db->where_in('description', $names)
foreach ($query->result() as $interestid) {
$res .= $interestid->interest_id;
$res .= ", ";
$res = substr($res, 0, -2);
$res .= ")";
return $res;

It translates my query to the following, hence why I have an error:

HAVING interests IN '(7)'

How do i remove the quotes around the (7) ?

Answer Source

You can disable the escaping.

If you are using a database that CodeIgniter escapes queries for, you can prevent escaping content by passing an optional third argument, and setting it to FALSE.

$this->db->having('user_id',  45);  // Produces: HAVING `user_id` = 45
$this->db->having('user_id',  45, FALSE);  // Produces: HAVING user_id = 45

Not sure how to implement this when just passing a single array as one argument, but you need the additional FALSE parameter.


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download