Support for receiving functions implemented in a single request

Is there a way to optimize the following for a single 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(); 
+4
source share
2 answers

You can skip the SELECT part and $num_rows .

Enough: UPDATE : the record (s) found according to your predicate (i.e. WHERE ) will be updated as indicated in the SET clause; if no records are found, the table / relvar will be "as is" (i.e. the update will not happen.)

Hope this helps.

+5
source

You do not need to check for a username / email / amount in the system. Just use $ num_rows1 to determine if you are updating an entry in the database - this will tell you if there was a match or not.

However, if the username, email, and amount are not enough to provide a unique record (i.e., it is likely that you will match more than one record in the database), this will not work. In this case, I would recommend finding a set of fields that together represent a unique key and ensure that you check them as part of the update.

+2
source

All Articles