Jump to content

Condense MySQL Query


HDFilmMaker2112

Recommended Posts

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.