感谢您的检查。所有有用的答案/评论都已投票。 我有以下代码,它可以完成这项工作,但是imo效率不高。我认为效率不高的原因是因为我使用fetchAll +循环,即使我知道查询将返回1或没有记录。PDO在PHP中如何改进这个PDO mysql代码
//assume the usual new PDO, binding, and execute are up here
$myval = "somevalue";
$res = $stmt->fetchAll(PDO::FETCH_ASSOC);
if (!$res) {
//no record matches
//BLOCK A CODE HERE
} else {
//found matching record (but always going to be 1 record, no more)
foreach($res as $row) {
if ($myval == $row['val']){
//myval is the same as db
//BLOCK B CODE HERE
} else {
//myval is different from db
//BLOCK C CODE HERE
}
}//foreach
}
如何提高其去除的foreach和使用fetchall的笨重的外观(考虑我知道它总是会只有1点或0的记录)?但我仍然需要类似的检查点,所以我可以执行相同的BLOCK A
BLOCK B
BLOCK C
,因为我目前的逻辑需要它。
+1使用异常! – ChrisR 2009-10-15 17:56:39
是的,+1。 – Chris 2009-10-15 20:44:51