Jump to content

Database Query Error Handling


webref.eu

Recommended Posts

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

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.

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();

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.