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

时间:2017-10-19 10:35:56

标签: php mysql codeigniter datamapper codeigniter-datamapper

如何获取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,datamapper库。

谢谢!!!

3 个答案:

答案 0 :(得分:2)

只需使用 count()这样的功能

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

答案 1 :(得分:1)

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

答案 2 :(得分:0)

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

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

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