RE: MySQL Update Quandry

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Please forgive me as I've been working with C# recently so my PHP is a
little rusty. But if I'm reading this correctly...

If your query fails outright, your function is going to return false:

"if (!(@ mysql_query($query, $connection))) {
       $this->errors = array(mysql_errno(), mysql_error());
       $error = new Error();
       $error->logError($this->errors);
       return false;
     }"

At that point, the script will exit and you'll see the error information in
your error log. Because the function/script will exit when it encounters a
return statement, you wouldn't ever get to the line of code with the
mysql_affected_rows() function in it if your SQL did, indeed, error out. It
would write to the error log, return false and exit.

If the query was successful, but no data was changed, all you're returning
is false. That, as you said, doesn't help much. Plus, it's the same result
you're returning for a failed query. Pretty ambiguous. Instead of returning
false from these functions, maybe returning some sort of status instead
would help. For example, if the query fails due to an error write to the
error log as you're currently doing, but return -1 instead of false. If the
query succeeds, but no data was changed, return a 0 instead of false since
false isn't exactly what the result would mean, but 0 is a little closer to
what you really mean. If the query was successful, you could still return
true or you could return the number of rows affected if you care to have
that information.

In the calling code, you would have a function to receive the statuses above
and display more meaningful information. For example,

(pseudocode, of course)

if($queryresult == -1){
	$msg = "The query failed. Check the error log.";
}elseif($queryresult == 0){
	$msg = "The query was successful, but no rows were altered."
}else{
	$msg = "The query was successful. ".$queryresult." rows were altered.";
}
echo $msg;

Hope this helps.

Rich

-----Original Message-----
From: maestro [mailto:maestroQC@xxxxxxxxx]
Sent: Friday, November 25, 2005 1:11 PM
To: php-db@xxxxxxxxxxxxx
Subject:  MySQL Update Quandry


Greetings,

I am doing an update to a table as such:

class DB {
....

   function updateMember($email, $password, $postalCode,
                         $language, $id, $word) {

   ...
     if (!(@ mysql_query($query, $connection))) {
       $this->errors = array(mysql_errno(), mysql_error());
       $error = new Error();
       $error->logError($this->errors);
       return false;
     }

     if (mysql_affected_rows() == 0) {
       return false;
     }

     return true;
   }
}



If the user supplies a wrong $word the mysql_affected_rows will be 0 and
  the method will return false. This is fine.
My problem arises when the user supplies data that has not changed. The
mysql_affected_rows will be 0 as well.
How can I know the difference between an update that fails due to a
failed where clause and one that updates nothing?

TIA,
Glenn

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux