Group: http://groups.google.com/group/codeigniter-kenya/topics
Peter Karunyu <pkar...@gmail.com> Aug 04 06:28PM +0300
--
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;
*
--
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.