Re: {CodeIgniter Kenya} Digest for codeigniter-kenya@googlegroups.com - 2 Messages in 1 Topic

17 views
Skip to first unread message

Nd'wex Common

unread,
Aug 3, 2013, 7:13:31 AM8/3/13
to codeigni...@googlegroups.com
If my memory serves me right if the query fails due to some error there will be an error message and it "halts" whereas in the event there no records num_rows() returns false/0.

$rs = $this->db->query(some query here);
if($rs->num_rows() > 0)
{
   $rs = $rs->result();   //returns an array of objects
   foreach($rs as $objects)
   {
      //do something with the data here
   }

   return $something;
}

return FALSE;


On Sat, Aug 3, 2013 at 10:19 AM, <codeigni...@googlegroups.com> wrote:

Group: http://groups.google.com/group/codeigniter-kenya/topics

    Isaak Mogetutu <imog...@gmail.com> Aug 02 12:34AM -0700  

    I'm eeked out by this code segment:
     
    if ($result == FALSE) {
    return FALSE;
    }

    if ($result->num_rows() == 0) {
    return FALSE;
    }
     
    Quick question if $result == false can $result->num_rows() != 0 thats why
    the double checks for false
    Instead can you check if its true load master array else return false
     
    // Not tested just my thoughts
    function count_distinct_values($table_name = '', $column_name = '')
    {
    $result = $this->db->query("SELECT $column_name, count(*) AS
    value_count FROM $table_name GROUP BY $column_name");

    if ($result) {
    $master_array = array();
    foreach ($result->result_array() as $row) {
    $master_array[$row[$column_name]] = $row['value_count'];
    }

    return $master_array;
    }

    return FALSE;
    }
     
     
     
    On Thursday, 1 August 2013 16:49:14 UTC+3, Peter Karunyu wrote:

     

    Peter Karunyu <pkar...@gmail.com> Aug 02 10:39AM +0300  

    I got into the habit of separating num_rows() from the other one coz I
    sometimes need to differentiate from a query failing (which is super bad)
    to a query not returning any data (which is ok).
     
    But in the example above, there's no need for that so yes, your suggestion
    is better
     
     
     
    --
    Regards,
    Peter Karunyu
    -------------------

     

--
You received this message because you are subscribed to the Google Groups "CodeIgniter Kenya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to codeigniter-ke...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Peter Karunyu

unread,
Aug 4, 2013, 11:28:05 AM8/4/13
to codeigni...@googlegroups.com
Using the world database, I have run this query which should return 209 rows:
$rs = $this->db->query("SELECT * FROM country");

And var_dump($rs) gives:
object(CI_DB_mysql_result)#14 (8) { ["conn_id"]=> resource(7) of type (mysql link persistent) ["result_id"]=> resource(8) of type (mysql result) ["result_array"]=> array(0) { } ["result_object"]=> array(0) { } ["custom_result_object"]=> array(0) { } ["current_row"]=> int(0) ["num_rows"]=> int(239) ["row_data"]=> NULL }

Now, if I run this query which should return zero results:
$rs = $this->db->query("SELECT * FROM country WHERE CODE='ABC'");

var_dump($rs) gives:
object(CI_DB_mysql_result)#14 (8) { ["conn_id"]=> resource(6) of type (mysql link persistent) ["result_id"]=> resource(7) of type (mysql result) ["result_array"]=> array(0) { } ["result_object"]=> array(0) { } ["custom_result_object"]=> array(0) { } ["current_row"]=> int(0) ["num_rows"]=> int(0) ["row_data"]=> NULL } 


So, i think a query that returns no results does NOT return false, it returns the same object a successful query does, only that the sub-objects i.e. $rs->result(), do not have any data.

If I run this erroneous query:
$rs = $this->db->query("SELECT * FROM non_existent_table");


CI throws a database error such as:
A Database Error Occurred
Error Number: 1146
Table 'world.non_existent_table' doesn't exist
SELECT * FROM non_existent_table
Filename: /Users/Peter/Sites/ci_test/controllers/welcome.php
Line Number: 22


Now, in a production environment, I don't want to be showing such a descriptive error to users, I want to show a different error message, AND log the occurrence of that said to the log files, and possibly send myself an email.

To achieve this:
1. The ENVIRONMENT in index.php will be set to PRODUCTION
2. In config/database.php, $db['default']['db_debug'] = TRUE; will be set to FALSE.
3. In config/config.php, $config['log_threshold'] = 0; will be set to at least 1.

Therefore, modifying your code slightly, I end up with this:

$rs = $this->db->query("SELECT * FROM non_existent_table");

if ($rs == FALSE) {
    log_message('error', "DB Error: (".$this->db->_error_number().") ".$this->db->_error_message(). ' Path: '.$this->uri->uri_string());
    show_error('Sorry, a database error occurred, Tech Support has been notified. Please try again later.');
    return FALSE;

}

if($rs->num_rows() > 0)
{
    $rs = $rs->result();   //returns an array of objects
   
    foreach($rs as $objects)
    {
        //do something with the data here
    }

    return $something;
}

return FALSE;

Reply all
Reply to author
Forward
0 new messages