By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,969 Members | 1,668 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,969 IT Pros & Developers. It's quick & easy.

How to check whether query executed or not

Vkas
P: 78
i had created a form ! having
three text box

Old password
New password
Confim new password!!


it is being validated by javascript with the following fuction

Expand|Select|Wrap|Line Numbers
  1. <script language="javascript">
  2.  
  3. function checkempty(obj,msg){
  4.     if(obj.value==''){
  5.         alert(msg);
  6.         obj.focus()
  7.         return false;
  8.     }
  9.     return true;
  10. }
  11. function confirmpass(obj,obj1,msg){
  12.     if(obj1.value!=obj.value){
  13.         alert(msg);
  14.         obj1.select()
  15.         obj1.focus()
  16.         return false;
  17.     }
  18.     return true;
  19. }
  20. function chkChangePass(){
  21.     if(checkempty(document.frmChangePass.oPass,"Information: Enter Old Password")==false) return false;
  22.     if(checkempty(document.frmChangePass.NPass,"Information: Enter New Password")==false) return false;
  23.     if(checkempty(document.frmChangePass.CNPass,"Information: Confirm New Password")==false) return false;
  24.     if(confirmpass(document.frmChangePass.NPass,document.frmChangePass.CNPass,"Information: New and Confirm Password should be same")==false) return false;
  25.     return true;
  26. }
  27. </script>
  28.  
The form code is as follow
Expand|Select|Wrap|Line Numbers
  1. <form name="frmChangePass" method="post" action="Changepass.php?action=Confirm&amp;id=1" onSubmit="return chkChangePass();">
  2.  
  3.           <table width="100%" border="0" cellspacing="2" cellpadding="1">
  4.               <tr bordercolor="#CCCCCC">
  5.                 <td width="30%">Old Password:</td>
  6.                 <td width="70%" align="left" valign="middle"><input name="oPass" type="password" onfocus="this.select();" value="<?php if(isset($_REQUEST["oPass"])){print $_REQUEST["oPass"];}?>" style="BORDER-WIDTH:1;BORDER-STYLE:dashed;border-color:000000;background:eeeeee;font-size:10px;color:000000;height:20;width:150" id="oPass" size="54" /></td>
  7.               </tr>
  8.               <tr bordercolor="#CCCCCC">
  9.                 <td>New Password: </td>
  10.                 <td align="left" valign="middle"><input name="NPass" type="password" onfocus="this.select();" value="<?php if(isset($_REQUEST["NPass"])){print $_REQUEST["NPass"];}?>" style="BORDER-WIDTH:1;BORDER-STYLE:dashed;border-color:000000;background:eeeeee;font-size:10px;color:000000;height:20;width:150" id="NPass" size="54" /></td>
  11.               </tr>
  12.               <tr bordercolor="#CCCCCC">
  13.                 <td>Confirm New Password: </td>
  14.                 <td align="left" valign="middle"><input name="CNPass" type="password" onfocus="this.select();" value="<?php if(isset($_REQUEST["CNPass"])){print $_REQUEST["CNPass"];}?>" style="BORDER-WIDTH:1;BORDER-STYLE:dashed;border-color:000000;background:eeeeee;font-size:10px;color:000000;height:20;width:150" id="CNPass" size="54" /></td>
  15.               </tr>
  16.  
  17.               <tr bordercolor="#FFFFFF" bgcolor="<?php echo $rClr?>">
  18.                 <td align="left" valign="middle" bordercolor="#CCCCCC" bgcolor="<?php echo $rClr?>" class="welcome">&nbsp;</td>
  19.                 <td align="left" valign="middle" bordercolor="#CCCCCC" bgcolor="<?php echo $rClr?>" class="Vkasimp"><input type="submit" name="Submit2" value="Change Pass" style="BORDER-WIDTH:1;BORDER-STYLE:double;border-color:000000;background:eeeeee;font-size:10px;color:000000;height:20;width:150" /></td>
  20.               </tr>
  21.             </table>
  22.             </form> 
  23.  
  24.  

according to action of the form on submission the below code is executed
it get the id,old password and new password
and execute the update query

Expand|Select|Wrap|Line Numbers
  1.  
  2. <?php }if($action=="Confirm"){
  3. $id=$_GET['id'];
  4. $oldpassword=$_POST['oPass'];
  5. $CNpassword=$_POST['CNPass'];
  6.  
  7. $update= "Update `users` Set `user_pass`='$CNpassword' Where `campus_id`=1 AND `user_pass`='$oldpassword'";
  8. $result=mysql_query($update) or die(mysql_error()); 
  9.  
  10. php echo "<strong>Password successfully changed</strong>"; 
  11. }
  12.  

if the old password is wrong or unmatched the query does not executes and password is not changed in the data base


i want to make check that if the query is not executed or the password is not changed due to unmatched old password than it should display


old password incorrect please correct it again

else it shows password changed successfully


my code simply show password changed successfully!! on both states!

i had used if else but not working
please help me
Apr 12 '10 #1
Share this Question
Share on Google+
13 Replies


Atli
Expert 5K+
P: 5,058
Show us what you tried.

When you want to check how many rows were successfully updated with an UPDATE query, you should use the mysql_affected_rows function. People often mistakenly use the mysql_num_rows function, but that function only counts the number of rows returned, and an UPDATE query never returns any rows.
Apr 12 '10 #2

Vkas
P: 78
i have only 1 row in the table simply

can u tell what would be the return type of the mysql affected rows function ?

can it be done like this


Expand|Select|Wrap|Line Numbers
  1. <?php }if($action=="Confirm"){
  2. $id=$_GET['id'];
  3. $oldpassword=$_POST['oPass'];
  4. $CNpassword=$_POST['CNPass'];
  5.  
  6. $update= "Update `users` Set `user_pass`='$CNpassword' Where `campus_id`=1 AND `user_pass`='$oldpassword'";
  7. $result=mysql_query($update) or die(mysql_error()); 
  8. if(mysql_affected_rows ($result) == false)
  9. {
  10.  echo "<strong> Old password wrong please retype it again<</strong>"; 
  11. }
  12. else
  13. {
  14.  echo "<strong>Password successfully changed</strong>"; 
  15. }
  16. ?>
  17. </td>
  18.   </tr>
  19. </table>
  20. <?php }?>
Apr 12 '10 #3

Atli
Expert 5K+
P: 5,058
To quote the manual (the link I posted earlier
Meaning, if you are expecting the query to affect one row, you would check to see if the function returns 1. If it does not then the query did not work as expected.
Apr 12 '10 #4

Vkas
P: 78
can you elaborate a little more so that i can reach with a solution
Apr 12 '10 #5

Atli
Expert 5K+
P: 5,058
You use the mysql_affected_rows function on the result of a mysql_query call to find out how many rows the query affected.

In your case, you need to find out if the query affected any rows at all, so you use it to see if one or more rows were affected. If there were one ore more rows affected, you print the success message. Otherwise you print an error message.

In simple pseudo-code, you could write that as:
Expand|Select|Wrap|Line Numbers
  1. rowsAffected = mysql_affected_rows(result)
  2. if rowsAffected > 0
  3.     Print success message
  4. else
  5.     Print error message
Apr 12 '10 #6

Vkas
P: 78
my $result= mysql_query("Query")
rowsAffected = mysql_affected_rows($result)
if rowsAffected > 0
Print success message
else
Print error message

it gives error

Warning: mysql_affected_rows(): supplied argument is not a valid
Apr 12 '10 #7

code green
Expert 100+
P: 1,726
This is a common error,
whereas mysql_num_rows expects a result resource,
mysql_affected_rows() expects a link (database) resource.
i.e the return from mysql_connect()
Apr 12 '10 #8

Atli
Expert 5K+
P: 5,058
O yea, that's right. I forgot that it took the database link, rather than the query result. Haven't worked with the old mysql functions in ages :)
Apr 12 '10 #9

Vkas
P: 78
so any other suggestion!!

mysql_query returns 1 on both condition whether the row is effected or not
Apr 12 '10 #10

Atli
Expert 5K+
P: 5,058
Another suggestion isn't needed. The one we've been talking about works fine, you just need to use the database link in the mysql_affected_rows() function rather than the query result.
Apr 12 '10 #11

Vkas
P: 78
ok i had got the solution with another logic thanks for you both for cooperation

i had checked firstly the old password from the database by fetching the value

then i compared and used if else simply
Apr 12 '10 #12

dlite922
Expert 100+
P: 1,584
Also keep in mind, if the user sets the password to his old password (i.e. you update a row, say...first_name to John when it's already set to John) the affected rows will be zero even though in some cases this is what you expect, "a blind update".

So I wouldn't build an if statement around an update query that expects it to always return a value greater than zero.
Apr 12 '10 #13

NettSIte
P: 7
Just a thought - basing an update on "where password =" is a tad risky. If your passwords are not unique, you will update all records with the same password, and if passwords are unique, you have opened a possibility for someone to figure out existing passwords.
Apr 13 '10 #14

Post your reply

Sign in to post your reply or Sign up for a free account.