Solved

trying to recognize when UPDATE query is done?

Posted on 2007-03-25
7
226 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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 …

733 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