mysql - Count and performs an insert or an update -


hi want know if possibile thing in 1 query.

$qrystring="select * votes userid='$userid' , messageid='$messageid'";   $qry = $pdo->prepare($qrystring); $qry->execute();  $resultarray=$qry->fetchall(); $num=count($resultarray);  if ($num==0) {$qrystring= "insert votes (userid, value, messageid) values ('$id', '$value', '$messageid')" }  else { $qrystring = "update votes set value='$value', userid='$id' , messageid='$messageid' " } 

doing perform @ least 2 queries. it's "grotesque" method (with performance ripercussions) or right way?

assumine userid has unique index:

insert votes (userid, value) values (:id, :value) on duplicate key update value = values(value); 

also, since you're using pdo, should using placeholders in prepared statement, not interpolating variables, showed above.


Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -