Welcome Guest, Not a member yet? Register   Sign In
Message: Undefined property: Blog::$db
#1

[eluser]Unknown[/eluser]
I am not good at English but I will try. I am using tutorials "Blog in 20 min" and i am receiving an undefined variable error message on line 22 which is:

Fatal error: Call to a member function get() on a non-object in C:\WebServ\httpd\CodeIgniter\system\application\controllers\blog.php on line 22

controller file:
Code:
class Blog extends Controller{

    function Blog(){

        parent::Controller();

        $this->load->scaffolding('entries');

        $this->load->helper('url');

        $this->load->helper('form');

    }

    function index(){

        $data['title'] =    "My title";

        $data['heading'] = "My heading";

        $data['query'] = $this->db->get('entries');

        $this->load->view('blog_view', $data);
    }
}


and view file:

Code:
<?=$heading?>
<br>

&lt;?php foreach($query->result() AS $row): ?&gt;

&lt;?=$row->title?&gt;

<br>

&lt;?=$row->body?&gt;

&lt;?=anchor('blog/comments', 'Comments');?&gt;

<hr>

&lt;?php endforeach; ?&gt;



some ideas?
#2

[eluser]andyd[/eluser]
do you not need to load the model in the index function?

just an idea, but otherwise where is the sql query / result coming from?
#3

[eluser]andyd[/eluser]
actually, do you not need to load the database helper?
#4

[eluser]chobo[/eluser]
Check if this is set to true in your database config file. As far as I know db->get is an active record thing, so it needs to be set to use it.

Code:
$db['default']['active_r'] = TRUE;

You can also auto load the db for each page by setting it in the autoload.php

Code:
/*
| -------------------------------------------------------------------
|  Auto-load Libraries
| -------------------------------------------------------------------
| These are the classes located in the system/libraries folder
| or in your system/application/libraries folder.
|
| Prototype:
|
|    $autoload['libraries'] = array('database', 'session', 'xmlrpc');
*/

$autoload['libraries'] = array('database')
#5

[eluser]Unknown[/eluser]
Thank you, it works Smile !
#6

[eluser]scatheroy[/eluser]
This same problem is happening to me, but with the added problem that the 'database' library doesn't seem to be included in the newest publish of CI.

Hence, using this
Code:
$autoload['libraries'] = array('database');
gives me a blank page; no code is generated->application broken.

But

using this
Code:
$autoload['core'] = array('database');
(which is deprecated) renders the output page properly-> application works.

But

using this method gives me
Code:
Message: Undefined property: Offer::$db
.
when I try to run a database query. The code I've written for the actual query is simple : $data['query'] = $this->db->get('tablename');


Anyone know how to deal with this?

Thanks much.
#7

[eluser]Michael Wales[/eluser]
scatheroy
Are you running the most recent version of CI?
Are you sure the $autoload['libraries'] line in config.php is correct (syntax)?
Have you configured the server correctly in database.php?
Have you loaded the database library within the controller to see if it works that way?
#8

[eluser]scatheroy[/eluser]
I've looked deeper into the problem, and it looks like the php mysql drivers aren't being loaded, despite the fact that they are properly mentioned in my php.ini config.

I'm not exactly sure where to go from here; I'm a rails developer who's being moved over the PHP by my firm for 1 single project because the client doesn't want to move away from PHP.

Thanks for your help, but it looks like my problem, and not codeigniter.

I have a feeling I'm about to have a very long night.
#9

[eluser]Unknown[/eluser]
$autoload['libraries'] = array('database') ; works just fine... mate i reckon you are going too far away
#10

[eluser]woran[/eluser]
the same problem, using xampp 1.6.7 and CI 1.6.3 ... all conf is ok




Theme © iAndrew 2016 - Forum software by © MyBB