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
https://forums.phpfreaks.com/topic/239845-condense-mysql-query/
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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.