How to get count of the sql search query?

87 Views Asked by At

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!!!

3

There are 3 best solutions below

0
Rajeev Radhakrishnan On BEST ANSWER
if($u->exists())
{
 echo "Found" // Do something
}
else
{
 echo "Nothing found" //Do something
}
0
Galih Akbar Moerbayaksa On

Just using count() function like this

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

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

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