CodeIgniter Forums
Proper way to return sql results - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Proper way to return sql results (/showthread.php?tid=32792)



Proper way to return sql results - El Forum - 08-04-2010

[eluser]GeXus[/eluser]
Hey guys, I've been handling my queries like this:

Code:
function getSomething(){
  
  $query = $this->db->query("QUERY");
  return ($query->result_array()) ? $query->result_array() : false;

}

By doing it this way (checking if its true first, then returning it) cause the query to be run twice? Would it be better to store the query in a variable, then do the check on that variable?


Proper way to return sql results - El Forum - 08-05-2010

[eluser]WanWizard[/eluser]
The query runs only once, that's not a problem.

It does uses some memory and processing as you fetch the resultset twice. You can do
Code:
if ( $query->num_rows() )
{
    return $query->result_array()
}
else
{
    return FALSE;
}