简体   繁体   English

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

[英]How to get count of the sql search query?

How to get count of the sql search query 如何获得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);

How to get count of the query results in $u->query($sql); 如何在$u->query($sql);获取查询结果的计数$u->query($sql); .I need to set a validation on it. 我需要对此进行验证。 If the query results count is 0 , i need to set a message. 如果查询结果计数为0,则需要设置一条消息。 Im using PHP Codeigniter ,datamapper library. 我正在使用PHP Codeigniter的datamapper库。

Thank you!!! 谢谢!!!

Just using count() function like this 只是像这样使用count()函数

...
$result = $u->query($sql);
$total_data = count($result);
if($u->exists())
{
 echo "Found" // Do something
}
else
{
 echo "Nothing found" //Do something
}
$result = $this->db->get();

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM