CodeIgniter Forums
How to avoid ugly 'Error Number: 1146' (database) errors - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: How to avoid ugly 'Error Number: 1146' (database) errors (/showthread.php?tid=58902)



How to avoid ugly 'Error Number: 1146' (database) errors - El Forum - 08-02-2013

[eluser]Zeff[/eluser]
Dear all,

Everyone has ever seen an 'Error Number: 1146' error (e.g. a call to a non existing table or an existing table in the wrong database...) while coding in models. Is there anyone who knows how to avoid this ugly error and show a nice error message to the user without breaking the rest of the code?

I tried it by using a try-catch exception block, but it doesn't help...
<code>
try {
$this->load->model('some_model', 'mymodel'); // During this load, error is thrown
$temp['results'] = $this->mymodel->get_all_results();
} catch (Exception $e) {
echo '<p>Some gentle message...</p>';
}
</code>

Thanks in advance for suggestions and help!

Zeff


How to avoid ugly 'Error Number: 1146' (database) errors - El Forum - 08-02-2013

[eluser]jairoh_[/eluser]
can us show us your query sir?


How to avoid ugly 'Error Number: 1146' (database) errors - El Forum - 08-02-2013

[eluser]Zeff[/eluser]
Hi jairoh_,

The called (working) model method in my case is:
<code>
public function get_all_results($id='')
{

if($id!='')
{
$id = intval($id);
$query = $this->db->get_where('mytable', array('id' => $id));
}
else
{
$this->db->order_by('year', 'acs');
$query = $this->db->get('mytable');
}

if($query->num_rows() > 0) return $query->result_array();

return false;
}

</code>

Cheers,

Zeff


How to avoid ugly 'Error Number: 1146' (database) errors - El Forum - 08-02-2013

[eluser]boltsabre[/eluser]
Have a look at this:

http://stackoverflow.com/questions/10679714/codeigniter-display-custom-view-instead-of-database-error-message
regarding disabling db error display in your live server and editing the default error page.

Also, these lines:
$id = intval($id);
$query = $this->db->get_where(‘mytable’, array(‘id’ => $id));

Could be replaced with:
$query = $this->db->get_where(‘mytable’, array(‘id’ => intval($id)));

Saves you a line of code and reduces the chance of bugs


How to avoid ugly 'Error Number: 1146' (database) errors - El Forum - 08-02-2013

[eluser]Zeff[/eluser]
Hi boltsabre, thanks for the tip. I'm not familiar with the error pages in application\errors, but this can solve my problem.

Cheers,

Zeff