2017-10-19 91 views
-1

如何獲得SQL搜索查詢如何獲得sql搜索查詢的計數?

$u = new user(); 

$sql = "SELECT a.id FROM `accounts` AS a LEFT JOIN `users` AS u ON a.id = u.id WHERE a.id IS NOT NULL "; 

$gender = $this->input->post('gender'); 

if($gender != NULL) 
{ 
    $sql .= "AND u.gender = '".$gender."' "; 
} 

$u->query($sql); 

如何獲得在$u->query($sql);。我在查詢結果的數量需要設置一個驗證它的計數。如果查詢結果計數爲0,則需要設置消息。我使用PHP Codeigniter,數據映射器庫。

謝謝!!!

+0

'SELECT COUNT(*)FROM ...'?但是你應該使用準備好的語句。 –

回答

0
if($u->exists()) 
{ 
echo "Found" // Do something 
} 
else 
{ 
echo "Nothing found" //Do something 
} 
2

只需使用計數()函數這樣

... 
$result = $u->query($sql); 
$total_data = count($result); 
0
$result = $this->db->get(); 

For Codeigniter use $count = $result->num_rows(); // HERE IS YOUR COUNT 

For OOP PHP use $count = $result->num_rows;