Page 1 of 1

Is this error checking necessary?

Posted: Mon Oct 23, 2006 6:56 pm
by batfastad
Hello everyone

Is it worth doing this to check whether MySQL inserts have completed successfully?

Code: Select all

// CHECK FOR MYSQL ERROR
if ( mysql_error() != '') {
    $sql_error_msg = mysql_error();
    $sql_error_code = mysql_errno();
    $error = 1;
    $error_msg .= "Could not add material log item<br />\nMySQL error. Error code $sql_error_code $sql_error_msg<br />\n";
}
Normally I guess MySQL gives its own error message if something goes wrong so I guess this is redundant, but just wondered what you guys thought??!


Thanks

Ben

Posted: Tue Oct 24, 2006 2:43 am
by CoderGoblin
Some error checking is always useful. Imagine if for some stupid reason your database structure changes and an insert fails. You need to catch this. I never output SQL errors to general users (admin level users ok) as it can give them help to perform SQL injection attacks.

As well as checking the SQL error I also use mysql_affected_rows. This will show how many rows have been affected/inserted which is useful for updates and inserts

Posted: Tue Oct 24, 2006 5:50 am
by timvw
I'd suggest that you use the return value of mysql_query instead of using mysql_error in order to determine that an error has occurred... From there i agree with CoderGoblin that mysql_affected_rows comes in handy...