Link to home
Start Free TrialLog in
Avatar of tyweed420
tyweed420

asked on

trying to recognize when UPDATE query is done?

I'm trying to recognize when an UPDATE SET COMMAND has been carried out by query. However, I'm getting results sometimes where i can see itupdated but my mysql_affected_rows() == 0 . Any ideas why?
How can i fix this?

 $sql = "UPDATE paypal_ipn SET savedFormData ='$savedFormData',petName = '$petName' WHERE userID = '$userID'";

  $r = mysql_query($sql);

  if(!$r)//bad sql command
  {
      $err=mysql_error();
      print $err;
      exit();
  }
  /*not working rightelse if(mysql_affected_rows() == 0)
  {
                print " <center>";
                  print "Unable to save your form info to our database.";
                  print"</center>";

  }*/
  else
  {
                print " <center>";
                    print "Your form info has been saved to our database.";
                  print"</center>";
  }
Avatar of gamebits
gamebits
Flag of Canada image

if(mysql_affected_rows($r)==0){
Are you sure your mysql_quey() line is finishing successfully? You can change it to:

  $r = mysql_query($sql) or die("Query $sql failed: " . mysql_error());

Normally, you would think such an error would be caught by your next line ('if(!$r)'), but if this code block occurs in a loop, a valid query response for $r may already exists, so the check will succeed even if the most recent mysql_query() did not. In this case, you can encounter the strange behaviour of seeing an update (from the previous time through the loop), but still receive the message "Unable to save your form...".

Do you think this may match your situation?

@gamebits: A MySQL resource link can be passed to the mysql_affected_rows() function, but not a query response.
mysql_query() returns false on error.

if($r === false)
{
  echo "Error!!!!";
}
elseif(!(mysql_affected_rows() == 0))
{
 echo "RECORD WAS SAVED!";
}
else
{
 echo "RECORD WAS NOT SAVED!";
}
Avatar of tyweed420
tyweed420

ASKER

Sage, that if else statement gives me

RECORD WAS NOT SAVED!  
 but the database is saving it? any ideas whats going on here
ASKER CERTIFIED SOLUTION
Avatar of BrianGEFF719
BrianGEFF719
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Forced accept.

Computer101
EE Admin