Welcome Guest, Not a member yet? Register   Sign In
Handling mysql errors with active record
#1

[eluser]BlueCamel[/eluser]
How do you handle mysql records from active record? Is there a small example available? In my Lib I have the following:


Code:
function section_add( $param )
{
    $this->CI->db->insert( $param );
    return $this->CI->db->insert_id();
}

So I'm thinking if the insert fails it would be nice to catch and handle the mysql error. I'm a little lost on where I add the error checking in here. Can I expect the insert to return FALSE? What about the error code and error string from mysql?

Thanks.
#2

[eluser]tonanbarbarian[/eluser]
Admittedly the database is not the best documented of the main classes, but that is because there is so much functionality in it.

If you look in the code DB code you will see that insert returns a boolean.
Or you could check $this->db->affected_rows();

There is no code to get the error message from the database. Which I actually do not have a real problem with.
1. CI by default has full error checking on in PHP so it will display the error from mysql for you.
2. In the live site you should not be getting any errors or if you do you should be able to anticipate exactly what the error is

You can use
Code:
$this->db->display_error('my message');
to display your own error message.
#3

[eluser]BlueCamel[/eluser]
[quote author="tonanbarbarian" date="1200621689"]
There is no code to get the error message from the database. Which I actually do not have a real problem with.

1. CI by default has full error checking on in PHP so it will display the error from mysql for you.
2. In the live site you should not be getting any errors or if you do you should be able to anticipate exactly what the error is

You can use
Code:
$this->db->display_error('my message');
to display your own error message.[/quote]

Sure, the return values are easy enough. It would be helpful thuogh that if I didn't get a return value expected that I could check the sql error and see if it was due to a badly formed query, out of memory, or some other oddness. Of course, this would need to be sanitized into a user consumable error indicating if they should try again later or if they should give up and go home for the day.

Oh, I did find in the database config (iirc) where I can disable display sql errors, which is definitely helpful.
#4

[eluser]anjelika[/eluser]
Hello,
Is there a way to see the actual SQL that is generated by AR?
This could be useful for debugging purposes.
Thanks
#5

[eluser]Vince Stross[/eluser]
Code:
echo $this->db->last_query();
#6

[eluser]anjelika[/eluser]
Nice, thank you!




Theme © iAndrew 2016 - Forum software by © MyBB