0

I'm new to CI. And just wanted to know where did I go wrong. Seems that I can't connect to my database. So I tried loading the database in the controller, just to see if it can connect and I can do queries. I did configure the database.php and set everything up. But it displays as this.

A PHP Error was encountered

Severity: Notice

Message: Undefined property: Blog::$db

Filename: controllers/blog.php

contollers/blog.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Blog extends CI_Controller {

    public function index() {
        $this->load->model("blogmodel");
        $articles = $this->blogmodel->get_articles_list();

        echo $articles;
    }
}

blogmodel.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Blogmodel extends CI_Model {

    function get_articles_list() {
        $list = $this->db->query("SELECT * FROM foo");
        return $list;
    }
}

Hope you can help me. Thanks.

Boy Pasmo
  • 8,021
  • 13
  • 42
  • 67
  • I don't see your model load and you have a wrong comprehension using the MVC structure. When querying you should put all your syntax in the model – Drixson Oseña Nov 07 '13 at 02:24
  • I didn't use any model. Earlier in my code I did use load() to load my model. But it seems there's something wrong. I just want to try how to use queries to get data from database. – Boy Pasmo Nov 07 '13 at 02:27
  • Did you try adding the standard constructor to blog class? – Kai Qing Nov 07 '13 at 02:28
  • Then you need to call the contstructor of CI_Model but this setup will defeat the purpose of CI. You should practice separating Controllers and Model or just use pure PHP – Drixson Oseña Nov 07 '13 at 02:30
  • I see. So I need to extend CI_Model to query data from my database? – Boy Pasmo Nov 07 '13 at 02:33
  • 1
    Drixson is right about the general MVC practice of db interactions in models but CI doesn't care about that. If you have loaded the db model it should work in a controller, even if it is not pretty. Question is - have you autoloaded database at this point? Without the __construct() method defined in your class it may not inherit the properties of any autoloaded model though. – Kai Qing Nov 07 '13 at 02:33
  • And no, you don't need to extend CI_Model. – Kai Qing Nov 07 '13 at 02:34
  • Updated my post. Can you please check on it? That's my code earlier. – Boy Pasmo Nov 07 '13 at 02:36
  • You still don't have the costructor in the blog class, or the blogmodel. Look at this post and see the examples they provide: http://stackoverflow.com/questions/8342232/extending-the-controller-class-in-codeigniter - I'm not posting this as an answer cause I'm not positive about that exactly but I use CI a lot and never leave out the constructor method – Kai Qing Nov 07 '13 at 02:38
  • @KaiQing really really appreciated your help. I didn't autoloaded the library for database. Now my problem is to convert stdClass to string. Nevertheless, thank you very much. – Boy Pasmo Nov 07 '13 at 02:46
  • `$articles` would be a result object. You may want to try `foreach($articles->result() AS $article){ echo $article->some_property; }` – Kai Qing Nov 07 '13 at 02:48
  • Thanks you for your response. Can you please post it in the answer section? So I can upvote it and label it as the correct answer. Thank you. – Boy Pasmo Nov 07 '13 at 02:52

1 Answers1

1

So you have an answer to accept:

The initial issue was you were not autoloading the database library. In this file:

application/config/autoload.php

You would add the database to the list:

$autoload['libraries'] = array('database');

As for your string issue - get_article_list() is returning an object. You can use $articles->result() to iterate through the results, or use something like $articles->num_rows() to see how many results there were, etc.

So where you echo $articles, you may intend to echo something like this:

foreach($articles->result() AS $article)
{
    echo $article->title . "<br>";
}

Where $article->title is obviously replaced with an actual field from your article table.

Kai Qing
  • 18,793
  • 5
  • 39
  • 57