Welcome Guest, Not a member yet? Register   Sign In
How to avoid ugly 'Error Number: 1146' (database) errors
#1

[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
#2

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

[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
#4

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

http://stackoverflow.com/questions/10679...or-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
#5

[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




Theme © iAndrew 2016 - Forum software by © MyBB