Welcome Guest, Not a member yet? Register   Sign In
Check empty query
#1

[eluser]joke2k[/eluser]
Hi, this is my firt post in this forum.

I need a function that check if all sql command is empty (where, join... etc), before call $this->db->result().

For example:
Code:
...
$this->db->where('name', $name);
if (!is_query_empty()) {
   echo 'not empty';
}
...

what can i do?

tnx
#2

[eluser]danmontgomery[/eluser]
Code:
$query = $this->db->where('some_field', $some_value)->get();
if($query) {
    // Query successfully ran (invalid queries will return false)
    if($query->num_rows()) {
        // Query returned results
        foreach($query->result() as $row) {
            // Do work
        }
    }
}

You can of course combine those two if you don't need to check for both conditions separately.

Code:
if($query && $query->num_rows()) {
#3

[eluser]joke2k[/eluser]
hehehe sorry for my italian english!!

I'll try to explain me better.

I need the check , before that DB Library perform a db-query.
Reading the source, i think that to do this, i must check many variables :
Code:
var $ar_select                = array();
    var $ar_distinct            = FALSE;
    var $ar_from                = array();
    var $ar_join                = array();
    var $ar_where                = array();
    var $ar_like                = array();
    var $ar_groupby                = array();
    var $ar_having                = array();
    var $ar_limit                = FALSE;
    var $ar_offset                = FALSE;
    var $ar_order                = FALSE;
    var $ar_orderby                = array();
    var $ar_set                    = array();    
    var $ar_wherein                = array();
    var $ar_aliased_tables        = array();
    var $ar_store_array            = array();

something like this:
Code:
... extends DB_active_rec ...
function is_empty_query() {
return ($this->ar_distinct == FALSE && is_empty($this->ar_from) && is_empty($this->ar_join) .... && ...);
}
...

sounds good?
#4

[eluser]danmontgomery[/eluser]
Yep, looks like it.
#5

[eluser]joke2k[/eluser]
Final solution:

Code:
function is_empty_query() {
    return (
        $this->ar_distinct == FALSE &&
        is_empty($this->ar_from) &&
        is_empty($this->ar_join) &&
        is_empty($this->ar_where) &&
        is_empty($this->ar_like) &&
        is_empty($this->ar_groupby) &&
        is_empty($this->ar_having) &&
        $this->ar_limit == FALSE &&
        $this->ar_offset == FALSE &&
        $this->ar_order == FALSE &&
        is_empty($this->ar_orderby) &&
        is_empty($this->ar_set) &&
        is_empty($this->ar_wherein) &&
        is_empty($this->ar_aliased_tables) &&
        is_empty($this->ar_store_array)  
        );
}

i've put this code in my ORM library because cannot extends DB_active_rec.

tnx for your time.




Theme © iAndrew 2016 - Forum software by © MyBB