Solved

trying to recognize when UPDATE query is done?

Posted on 2007-03-25
7
239 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
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 

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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

615 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