我執行笨語句從表中選擇的行用下面的查詢:如何知道如果一個表中的任一行滿足條件
$result = $this->db->query('select * from table_name where cond1 and cond2);
但我怎麼會知道任何行被選中或不。我使用count($ result)來確定選中的行數。但是在兩種情況下它都返回1,即當一行滿足條件並且沒有行不滿足條件時。所以我在問是否有任何方法來檢查。謝謝。
這裏假設只有一行滿足cond1和cond2。
我執行笨語句從表中選擇的行用下面的查詢:如何知道如果一個表中的任一行滿足條件
$result = $this->db->query('select * from table_name where cond1 and cond2);
但我怎麼會知道任何行被選中或不。我使用count($ result)來確定選中的行數。但是在兩種情況下它都返回1,即當一行滿足條件並且沒有行不滿足條件時。所以我在問是否有任何方法來檢查。謝謝。
這裏假設只有一行滿足cond1和cond2。
你在找這個嗎?
$result ->num_rows();
它是這樣做的。例如,數據庫中只有一行同時滿足cond1和cond2。但是,當沒有行存在時,它也給出了1. – Joy
你可以在這裏粘貼你的條件。或添加鏈接到截圖。 –
$結果= $這個 - > DB->查詢( '從TABLE_NAME其中COND1和COND2 SELECT COUNT(*)');
那麼你需要從返回行的第一列提取計數。
基礎上documentation正確答案是echo $result->num_rows;
if($result->num_rows()>0){
//One or more rows satisfies the condition
}else {
//Zero rows satisfies the condition
}
這將是看到完整的查詢是如何寫的,投了兩個有用的where子句這樣的,它與結合他們:
$this->db
->select ('*')
->from('table_name')
->where('table_name.column', cond1)
->where('table_name.column2', cond2);
$result = $this->db->get();
if ($result) {
//this means you have results, do something with them here
}
$count = $result->num_rows();
OR
$count = count($result->result());
你用兩種情況檢查,所以它顯示記錄時,兩種情況下滿足..代替和使用或... $結果= $ this-> db->查詢('select * from table_name where(cond1)or cond2)); ...然後它會檢查cond1或cond2 – nickle
@ nickle我剛給q用作示例。其實我想知道如何確定select語句是否選擇了任何行。 – Joy
你用什麼擴展連接數據庫? – George