Welcome Guest, Not a member yet? Register   Sign In
num_rows() problem in the count_all_results
#1

Hi guys,

I use the count_all_result function, and if I set a non existent column name in WHERE clause, then throw an error.
In the WHERE clause have dynamic column name, so incorrect column names may occur

Code:
$this->db->select('column_1');
$this->db->where('nonexist_col', 'anything');
$count = $this->db->count_all_results('table');

Error: Call to a member function num_rows() on a non-object
This is in the count_all_results function of DB_query_builder.

Code:
public function count_all_results($table = '', $reset = TRUE)
{
        ...
    ...

    if ($result->num_rows() === 0)
    {
        return 0;
    }
Because the query string wrong, the $result will have no num_rows function.
Here is a possible solution, but I don't want to overwrite the CI code:

Code:
if(is_object($result)){
        if ($result->num_rows() === 0)
    {
       return 0;
    }
}
else{
    return FALSE;
}

Is there any good solution for this?
Reply
#2

@CodeArt,

Why would you want an dynamic variable to return an invalid column name? Why don't you validate that the dynamic variable is a column name then if it is, then run your query.
Reply
#3

(02-06-2019, 07:35 PM)php_rocs Wrote: @CodeArt,

Why would you want an dynamic variable to return an invalid column name?  Why don't you validate that the dynamic variable is a column name then if it is, then run your query.

This table can be dynamically expanded with columns by user. I check the exists columns before query, but if something yet it goes wrong, I would like to check the error for own error handling, and I don't want default num_rows error message.
Reply
#4

@CodeArt,

Try using this... https://www.codeigniter.com/user_guide/d...ing-errors
Reply
#5

(02-07-2019, 08:22 AM)php_rocs Wrote: @CodeArt,

Try using this... https://www.codeigniter.com/user_guide/d...ing-errors

Thanks, I using this, and works fine with simple get queries, but not work with count_all_result for the previously written reasons
Reply




Theme © iAndrew 2016 - Forum software by © MyBB