Welcome Guest, Not a member yet? Register   Sign In
How to return a count on a custom query
#1

[eluser]drewjuk[/eluser]
Hi, I have been searching for hours how to return count_all_results on a custom query but what ever I do it fails here is the query with code:

Code:
$sql = "SELECT count(*) as `numrows` FROM `$table`
WHERE ( $datefield BETWEEN $datefrom AND $dateto) AND ($dquery)";

//echo $sql;
//$query = $this->db2->query($sql);
//echo $rows['numrows']
$rows = $this->db2->query($sql)->result();
echo "here".$rows."<br/><br/>";
echo $this->db2->last_query();
//echo "<Br/><br/>here = ".$count;

Any body have an idea how to make this work?

Thanks
#2

[eluser]drewjuk[/eluser]
I have managed to get it working using this, but I think it is rather slow and there must be a better way to do instead of using SELECT * FROM instead of select count:

Code:
$sql = "SELECT count(*) as `numrows` FROM `$table`
WHERE ( $datefield BETWEEN $datefrom AND $dateto) AND ($dquery)";
  

$query = $this->db2->query($sql);
return $query->num_rows();
#3

[eluser]Matalina[/eluser]
*edited out prior post*

You shouldn't have to do what you are doing with the select just use the $query->num_rows();

Code:
$sql = "my query here";
$query = $this->db->query($sql);
return $query->num_rows();

All you are doing in your code is selecting the count of for each row which will be the same and then counting the rows anyway when you return num_rows();




Theme © iAndrew 2016 - Forum software by © MyBB