Link to home
Start Free TrialLog in
Avatar of Lynn Thames
Lynn ThamesFlag for United States of America

asked on

In Codeigniter, how can I check for results from SQL query to database?

In my database model, I have a function that accepts an SQL statement as a parameter and returns the results in what I believe is an array of objects.

But if no records are found, it is returning "0" instead of an array of objects.

I know this can't be right because I get syntax errors when I try to test the returning results when no records are found.  

What would be the proper way to handle this so that the controller can check to see if any results are returned before calling the view?

Code is attached.

function Fetch_query($sql)
{
   $results = $this->masters->query($sql);
   if($results->num_rows()>0):
      $results = $results->result_array();
      return $results;
   else:
      return "0";
   endif;
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Lynn Thames

ASKER

Very helpful, I was able to complete the page with your help.

thank you!