我遇到的問題,像這樣無數次,一直有一種感覺,這不是最有效的邏輯和我似乎無法找到一個直接的答案:PHP分支優化和效率
在事件需要根據某些標準檢查數據庫,如果匹配成立,則更新記錄,如果匹配爲假,則轉到另一個語句,如果所有數據庫匹配都返回false,最後插入新記錄,是一種更有效的方法來優化當前的分支?
$id = *first query to look for id* //returns null if no match
if (is_null($_id)) { #no match
$id = *fallback query looking for id *; -->returns null if no match
}
if (is_null($id)) {#still no match
$id = *last fallback query looking for id*; -->returns null if no match
}
if (is_null($id)) { # no match was found, doesn't exist in DB so insert it
//code to INSERT the new record
} else { #match was found
//code to update the record*
}
我想我們需要查看您的實際查詢以提供一個很好的答案。 – Mooseknuckles