Solved

trying to recognize when UPDATE query is done?

Posted on 2007-03-25
7
219 Views
Last Modified: 2008-03-25
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>";
  }
0
Comment
Question by:tyweed420
7 Comments
 
LVL 28

Expert Comment

by:gamebits
ID: 18789707
if(mysql_affected_rows($r)==0){
0
 
LVL 24

Expert Comment

by:glcummins
ID: 18790129
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.
0
 
LVL 19

Expert Comment

by:BrianGEFF719
ID: 18790301
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!";
}
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:tyweed420
ID: 18796355
Sage, that if else statement gives me

RECORD WAS NOT SAVED!  
 but the database is saving it? any ideas whats going on here
0
 
LVL 19

Accepted Solution

by:
BrianGEFF719 earned 125 total points
ID: 18798029
if($r === false)
{
  echo "Error!!!!";
}
else
{
 if(mysql_affected_rows() > 0)
 {
 echo "RECORD WAS SAVED!";
 }
 else
 {
  echo "RECORD WAS NOT SAVED!";
 }
}
0
 
LVL 1

Expert Comment

by:Computer101
ID: 21207267
Forced accept.

Computer101
EE Admin
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question