Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dealing with Parentheses in Codeigniter Queries

I have the following block of code which is expected to return the count

    $sql = "SELECT sum(count) as count 
            FROM multipleowners WHERE owner = ? " . $localityquery;
    $queryarray = array($owner, $locality);
    $query = $this->db->query($sql, $queryarray);
    if ($query->num_rows() > 0)
    {
        $result = $query->row_array();
        $count = $result['count']; 
    }

But I am getting empty values when i try to print $count.

I have used print_r($this->db->last_query()); and I got the following query,

SELECT sum(count) as count FROM multipleowners WHERE owner = 'Davenports Harbour Trustee (2012) Limited' and locality = 'Auckland Central'

When I executed this query directly onto my Postgresql IDE i got the output of count as 2.

What and where could this query be gone wrong ? I doubt the existence of ( and ) in the WHERE clause. How do I fix this ?

Update

When I enabled the profiler I got the following query,

SELECT sum(count) as count 
            FROM multipleowners WHERE owner = 'Davenports Harbour Trustee (2012) Limited'  and locality = 'Auckland Central'

So obviously the problem exists on the ( and ) !!

like image 985
Deepak Avatar asked Nov 03 '22 05:11

Deepak


1 Answers

Bingo!! I have added the following line before I passed the variable $owner to the query and it worked,

$owner = html_entity_decode($owner);
like image 62
Deepak Avatar answered Nov 09 '22 06:11

Deepak