Solved

trying to recognize when UPDATE query is done?

Posted on 2007-03-25
7
191 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

708 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now