webref.eu Posted February 20, 2009 Share Posted February 20, 2009 If I have the following code: $AuthCode = makeSQLSafe($AuthCode); //database query $query = "UPDATE Users SET EmailConfirmed='1' WHERE AuthCode='$AuthCode'"; mysql_query($query); mysql_close(); How would I handle the situation where I get a query error returned, e.g. there's no matching AuthCode in the database? Thanks all Link to comment https://forums.phpfreaks.com/topic/146136-database-query-error-handling/ Share on other sites More sharing options...
premiso Posted February 20, 2009 Share Posted February 20, 2009 Do you need to handle it? $AuthCode = makeSQLSafe($AuthCode); //database query $query = "UPDATE Users SET EmailConfirmed='1' WHERE AuthCode='$AuthCode'"; $result = mysql_query($query); $rows = mysql_affected_rows($result); if ($rows < 1) { echo 'Nothing was changed.'; }else { echo $rows . " row(s) were updated."; } Simple as that. Whether that is an error, I do not know. Link to comment https://forums.phpfreaks.com/topic/146136-database-query-error-handling/#findComment-767172 Share on other sites More sharing options...
sford999 Posted February 20, 2009 Share Posted February 20, 2009 Or something like this: <?php $AuthCode = makeSQLSafe($AuthCode); //database query $query = "UPDATE Users SET EmailConfirmed='1' WHERE AuthCode='$AuthCode'"; $result = mysql_query($query); while($row = mysql_fetch_array($result)) { if($AuthCode != $row[AuthCode]) { // Codes didn't match echo 'Error'; } else { // All Good echo 'Congrats'; } } mysql_close(); ?> Link to comment https://forums.phpfreaks.com/topic/146136-database-query-error-handling/#findComment-767173 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.