Welcome Guest, Not a member yet? Register   Sign In
Proper way to return sql results

Hey guys, I've been handling my queries like this:

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?

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
if ( $query->num_rows() )
    return $query->result_array()
    return FALSE;

Theme © iAndrew 2016 - Forum software by © MyBB