Welcome Guest, Not a member yet? Register   Sign In
Got empty results from model, even though there're data in database
#2

Hi Gabriel, the problem is that you're not loading the database in Codeigniter. You need a line that says $this->load->database(); You can put that line in three places:

1.- In your application/config/autoload.php file.
2.- In the function where you access the database.
3.- In the constructor of your model file.

Personally, I prefer to put it in the constructor of my model files. If you put it in your autoload file, it will load on every single page. If you put it in the function that accesses the database, you'll have to include it in every function that accesses the database. But if you put it in the model file, then the database only gets loaded in controllers where you load that model file. So in your model file, you can do something like this.
PHP Code:
class Site_model extends CI_Model {

 
   function __construct()
 
   {
 
       parent::__construct();
 
       $this->load->database();
 
   }

 
   function getAll() {
 
       $data = array();
 
       $q $this->db->get('test');
 
       if ($q->num_rows() > 0) { 
That should do it.

Also, I figured you would have gotten an error message. Do you have errors turned on in your PHP.INI file?
Hey, don't work without a PHP debugger. Several free IDEs have this features built in. Two are NetBeans and CodeLobster. Without a debugger, it's like you're driving with a blindfold on -- you are going to crash!
Reply


Messages In This Thread
RE: Got empty results from model, even though there're data in database - by RobertSF - 03-18-2015, 07:39 PM



Theme © iAndrew 2016 - Forum software by © MyBB