HDFilmMaker2112 Posted June 20, 2011 Share Posted June 20, 2011 Can the following be condensed into one query? $username=sanitize($_POST['username']); $sql="SELECT * FROM $tbl_name WHERE username='$username' AND email='$email' AND amount='$donation_amount'"; $result=mysql_query($sql); $num_rows = mysql_num_rows($result); if($num_rows==1){ $sql1="UPDATE $tbl_name SET password='$new_password' WHERE username='$username' AND email='$email' AND amount='$donation_amount'"; $result1=mysql_query($sql1); $num_rows1=mysql_affected_rows(); Should I just throw out the SELECT and just use the update? $num_rows1 will equal 0 if it doesn't find an entry to update correct? Quote Link to comment https://forums.phpfreaks.com/topic/239845-condense-mysql-query/ Share on other sites More sharing options...
ebmigue Posted June 20, 2011 Share Posted June 20, 2011 Yep, you could omit the SELECT and $num_rows part. The UPDATE clause suffices: record(s) found according to your predicate (i.e., WHERE condition) will be updated as specified in the SET clause; if no records are found, the table/relvar will be "as is" (i.e., no update will take place.) Hope it helps. Quote Link to comment https://forums.phpfreaks.com/topic/239845-condense-mysql-query/#findComment-1232007 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.