Is this error checking necessary?

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
batfastad
Forum Contributor
Posts: 433
Joined: Tue Mar 30, 2004 4:24 am
Location: London, UK

Is this error checking necessary?

Post 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
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post 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
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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...
Post Reply