phpcodeigniterobjectquery-buildersanitization

Sanitize an object containing WHERE conditions and use with CodeIgniter's query builder


Is there a way to retrieve what the "where" clauses I have added in my query?

I use a foreach statement to add the columns I want to add in the where clause for the current query, but since the clause could be empty (no columns added, 0 iterations in the foreach) the query would retrieve all the records in the database because no restrictions where made.

Of course, I could use a boolean variable to know whether the foreach is making at least 1 iteration but I would like to know whether it is possible to retrieve information related to the query I'm making.

My code resembles this:

foreach ($columns as $column => $value) {
    if ($column != '') {
        $this->db->where($column, $value);
    }
}
$this->db->get('mytable');

Some explanation:

If $column is an empty string, I don't add it to the query.

$columns var is an object (stdClass).

So, if there is no "where" statement in the query, I don't want to retrieve any information with $this->db->get(). What I'm looking for is something like:

if (!empty($this->db->where_clauses()))
    $this->db->get('mytable');

Is there something like that?


Solution

  • Not directly: its a bit of a hack, but you can do this:

    if( ! empty($this->db->ar_where))
    

    Try that and see if it works.