Welcome Guest, Not a member yet? Register   Sign In
Error in $this->db->get()
#1

[eluser]Roey[/eluser]
Hello everyone.
I try to build a site with CodeIgniter framework.
when i try to use the
Code:
$this->db->get('animals');
i get an error:
Quote:Fatal error: Call to a member function get() on a non-object in C:\wamp\www\ci\system\application\controllers\register.php on line 14

I need help with that.

please tell me if you need more code from my project.

thank you
#2

[eluser]kaejiavo[/eluser]
you have to load the database library in autoload.php or in your controller $this->load->library('database')

Marco
#3

[eluser]Roey[/eluser]
here is my autoload file.
as you can see i load the db.

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

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

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

And when i try with the line:

Code:
$this->load->library('database');

i also get an error:
Quote:An Error Was Encountered

Unable to load the requested class: database

when i checked i dont have "database.php" in my "libraries" folder.
maybe this is the problem?
#4

[eluser]WanWizard[/eluser]
The fact that you attempt to load it doesn't mean it is. Loading can also fail if you have problems connecting to the database, if you database config is not correct, or if you don't have sufficient rights.

Check your CI logs (set the log threshold to 4 to see all messages).
#5

[eluser]Roey[/eluser]
well. i'm new to CodeIgniter and i dont know how/where to set this and check.
anyway,
when i try the "scaffolding" its working fine.

can you tell me how to check the CI logs and how to set the log threshold to 4?

thanks
#6

[eluser]WanWizard[/eluser]
Scaffolding is deprecated, and removed in CI 2.0. Don't have your apps rely on it, otherwise you won't be able to upgrade.

If that works, you DB seems ok, so we'll need to see your code to figure out what's wrong.

You change the log (and lots of other info) in config/config.php. I suggest you take a day or so to go through the user guide. It's excellent, and explains all these things...
#7

[eluser]danmontgomery[/eluser]
http://ellislab.com/codeigniter/user-gui...mples.html

It's not:

Code:
$this->load->library('database');
it's
Code:
$this->load->database();

In autoload, however, it does belong in libraries. There's even an example of this in the file. There's no "core" autoload section, you would have had to add it yourself, and I'm not sure why you'd do that.

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

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

[eluser]Unknown[/eluser]
I autoloaded the database library but I still get that non-object error mentioned above when I try to use $this-db in a model. When I do it in a controller everything works well. What could I be doing wrong?
My model looks like this:
Code:
<?php
class Membership extends CI_Model{
function validate(){
  $this->db->where('username', $this->input->post('username'));
  $this->db->where('password', md5($this->input->post('password')));
  $result = $this->db->get('members');
  //some code here
  }
}
}




Theme © iAndrew 2016 - Forum software by © MyBB