如何获得sql搜索查询的计数?

如何获得sql搜索查询的计数?

问题描述:

如何获得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 ...'?但是你应该使用准备好的语句。 –

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

只需使用计数()函数这样

... 
$result = $u->query($sql); 
$total_data = count($result); 

$result = $this->db->get(); 

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

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