-1

How to get count of the sql search query

$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); .I need to set a validation on it. If the query results count is 0 , i need to set a message. Im using PHP Codeigniter ,datamapper library.

Thank you!!!

Yahiya
  • 571
  • 2
  • 13

3 Answers3

2

Just using count() function like this

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

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

For OOP PHP use $count = $result->num_rows; 
Jawad A.
  • 73
  • 6