感謝您的檢查。所有有用的答案/評論都已投票。 我有以下代碼,它可以完成這項工作,但是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