EllisLab text mark
Advanced Search
     
Fatal error: Call to a member function query() on a non-object
Posted: 16 June 2011 05:45 AM
Joined: 2011-06-16
7 posts

Hi Guys,

Im tearing my hair out here as I cant do any database calls. Below are my database settings:

$active_group 'default';
$active_record TRUE;

$db['default']['hostname''localhost';
$db['default']['username''admin';
$db['default']['password''';
$db['default']['database''_NewAdmin';
$db['default']['dbdriver''mysql';
$db['default']['dbprefix''';
$db['default']['pconnect'TRUE;
$db['default']['db_debug'TRUE;
$db['default']['cache_on'FALSE;
$db['default']['cachedir''';
$db['default']['char_set''utf8';
$db['default']['dbcollat''utf8_general_ci';
$db['default']['swap_pre''';
$db['default']['autoinit'TRUE;
$db['default']['stricton'FALSE

The database connects fine without any problems.

I also have database autoloaded in autoload.php

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

But when I do a simple call like this:

public function _get(){
        $query 
$this->db->query('SELECT * FROM stock_categories');
        return 
$query->result();
    

I get the following error:

Fatal error: Call to a member function query() on a non-object in C:\wamp\www\greenModules\application\models\category_model.php on line 59

Please Help? What am i doing wrong?

Thanks In Advance

 
Posted: 16 June 2011 07:12 AM   [ # 1 ]   [ Rating: 0 ]
Avatar
Joined: 2009-12-08
1804 posts

You can’t use magic getter and setter methods in the model, that’s how CI connects the model to the controller.

https://bitbucket.org/ellislab/codeigniter-reactor/src/6b1e35f45ca5/system/core/Model.php#cl-47

 Signature 

@basdflasjk | BitAuth: Authentication and Role-based Permissions | Session Library Replacement


Please read the User Guide! (Upgrading from a previous version?)

 
Posted: 16 June 2011 07:42 AM   [ # 2 ]   [ Rating: 0 ]
Joined: 2011-06-16
7 posts
noctrum - 16 June 2011 11:12 AM

You can’t use magic getter and setter methods in the model, that’s how CI connects the model to the controller.

https://bitbucket.org/ellislab/codeigniter-reactor/src/6b1e35f45ca5/system/core/Model.php#cl-47

Thanks for the reply noctrum,

I have chnaged _get() to getAll() and the problem still occurs.

Im really stuck guys and could really do with your help.

Thanks in Advance.

 
Posted: 16 June 2011 08:44 AM   [ # 3 ]   [ Rating: 0 ]
Joined: 2011-06-16
7 posts

Anyone?

 
Posted: 16 June 2011 09:05 AM   [ # 4 ]   [ Rating: 0 ]
Avatar
Joined: 2011-03-08
417 posts

Either:
add this as a file and include it at the end of ./application/config/database.php
or

just append it to your ./application/config/database.php

// include 'database_DEBUG.php';

// copy the following code to ./application/config/database_DEBUG.php


/* file: ./application/config/database_DEBUG.php */

    // cosmetic    stuff
    
define('xxx',    '<p>Line:    %s : %s    </p>');
    
    
// show    parameters        
    
echo '<pre>';
        
print_r($db['default']);
    echo 
'</pre>';
    
    
// try to    connect    to database    using    username and password
    
$dbh = @ mysql_connect
                
(
                    
$db['default']['hostname'],
                    
$db['default']['username'],
                    
$db['default']['password']
                 
);
    if(
mysql_error())    
        echo 
sprintf(xxx,    __LINE__,    mysql_error());                
    
    
    
// try to    select database
    
$connected = @ mysql_select_db ($db['default']['database']);
    if (
mysql_error()) 
        echo 
sprintf(xxx,    __LINE__,    mysql_error());                
    
    
    
// display success/failure
    
$msg    =    '';    // default
    
if($connected)
    
{
        $msg 
'Connected    OK';
    
}//
    
echo sprintf(xxx,    __LINE__,    $msg);             

    
$query mysql_query('SELECT * FROM stock_categories');
    
    
// Check result
    // This shows the actual query sent to MySQL, and the error. Useful for debugging.
    
if (!$query)
    
{
      $message  
'Invalid query: ' mysql_error() . "\n";
      
$message .= 'Whole query: ' $query;
      die(
$message);
    
}     
    
echo 'Success';
    
// return $query->result()     
    
die();
    

// *********************************** 

 
 
 

 Signature 

Ongoing project:
  http://johns-jokes.com/joke-of-the-day/2010/May.html
My Hippy Trail:
  http://the-road-to-kathmandu.johns-jokes.com/
In case you forget:
  http://deformedweb.co.uk/php_variable_tests.php

 
Posted: 16 June 2011 10:42 AM   [ # 5 ]   [ Rating: 0 ]
Joined: 2011-06-16
7 posts

Thanks,

I know get the error

Invalid queryWhole queryResource id #26 

Your code above also checks a variable called $result at the end but that deos not appear anywhere else in your code.

What is the problem with this? Please guys I have no idea!

 
Posted: 16 June 2011 11:12 AM   [ # 6 ]   [ Rating: 0 ]
Avatar
Joined: 2009-06-19
6583 posts

You should always check to make sure you retrive data from a query!

public function get_all()
{
    $query 
$this->db->query('SELECT * FROM stock_categories');

    if (
$query->num_rows() > 0)
    
{
        
return $query->result();
    
}
    
else // you have an error - no records returned!
    
{
        
echo 'ERROR!';
        return 
FALSE;
    
}

InsiteFX

 Signature 

Certified State of CT Computer Programming Teacher.
Custom Designed Icons, eBook Covers Software Boxes. CD, DVD Etc. New iPhone® Tab Bar Icons and iPhone® Applications Icons.

Skype: insitfx

STOP! Before posting your questions, remember the WWW Golden rule:
What did you try? What did you get? What did you expect to get?

Input -> Controller | Processing -> Model | Output -> View

 
Posted: 16 June 2011 11:23 AM   [ # 7 ]   [ Rating: 0 ]
Avatar
Joined: 2011-03-08
417 posts
Creox - 16 June 2011 02:42 PM

Thanks,

I know get the error

Invalid queryWhole queryResource id #26 

Your code above also checks a variable called $result at the end but that deos not appear anywhere else in your code.

What is the problem with this? Please guys I have no idea!

Please accept my apologies for not testing the code. The script was copied and pasted.

The variable $result should be $query.

It looks like the connection to your database is OK and the problem is with the CI Active Record script.
 
 
 

 Signature 

Ongoing project:
  http://johns-jokes.com/joke-of-the-day/2010/May.html
My Hippy Trail:
  http://the-road-to-kathmandu.johns-jokes.com/
In case you forget:
  http://deformedweb.co.uk/php_variable_tests.php

 
Posted: 16 June 2011 11:59 AM   [ # 8 ]   [ Rating: 0 ]
Joined: 2011-06-16
7 posts

No problem.

Sorry Guys I still gaent solved this.

Im using 2.02 - The latest version. What do you recommend i should do?

 
Posted: 16 June 2011 12:09 PM   [ # 9 ]   [ Rating: 0 ]
Avatar
Joined: 2011-03-08
417 posts

Have you tried using another table name?

Also try http://localhost/phpmyadmin/index.php and check your database and tables.

edit:
Also try adding error checking and display errors:

public function _get()
{
  error_reporting
(-1);
  
ini_set('display_errors'1);

  
$query $this->db->query('SELECT * FROM stock_categories');
  return 
$query->result();


 
 

 Signature 

Ongoing project:
  http://johns-jokes.com/joke-of-the-day/2010/May.html
My Hippy Trail:
  http://the-road-to-kathmandu.johns-jokes.com/
In case you forget:
  http://deformedweb.co.uk/php_variable_tests.php

 
Posted: 16 June 2011 12:14 PM   [ # 10 ]   [ Rating: 0 ]
Joined: 2011-06-16
7 posts
John_Betong_002 - 16 June 2011 04:09 PM

Have you tried using another table name?

Also try http://localhost/phpmyadmin/index.php and check your database and tables.
 
 

John,

The tbales are in the database that i connect to succesffully.

When I exectue the following code

$query $this->db->query('SELECT * FROM users WHERE UserID = '.$userID.' LIMIT 1');
        return 
$query->result(); 

I expect to get a result back.

However I get the following

A PHP Error was encountered

Severity: Notice

Message: Undefined property: User_model::$db

Filename: models/user_model.php

Line Number: 14

Fatal error: Call to a member function query() on a non-object in C:\wamp\www\GREENStore\application\models\user_model.php on line 14

i have autoloaded the database in config/autoload.php

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

What else could it be? This is driving me insane.

I have just downloaded CI v2.01 at the error still occurs.

Please help.

Thanks

Ciaran

 
Posted: 16 June 2011 12:46 PM   [ # 11 ]   [ Rating: 0 ]
Avatar
Joined: 2011-03-08
417 posts

It looks as though your database class is not getting loaded.

Try setting your ./application/config/config.php and make sure everything is getting loaded.

$config['log_threshold''4'

 

Also try this:

error_reporting(-1);
  
ini_set('display_errors'1);

  
$this->load->database();

  
$query $this->db->query('SELECT * FROM users WHERE UserID = '.$userID.' LIMIT 1');
  return 
$query->result(); 

 
 

 Signature 

Ongoing project:
  http://johns-jokes.com/joke-of-the-day/2010/May.html
My Hippy Trail:
  http://the-road-to-kathmandu.johns-jokes.com/
In case you forget:
  http://deformedweb.co.uk/php_variable_tests.php

 
Posted: 16 June 2011 12:57 PM   [ # 12 ]   [ Rating: 0 ]
Joined: 2011-06-16
7 posts

Sorry John, That didnt work.

The ouput is now

A PHP Error was encountered

Severity
Notice

Message
Undefined propertyUser_model::$load

Filename
models/user_model.php

Line Number
18

Fatal error
Call to a member function database() on a non-object in C:\wamp\www\GREENStore\application\models\user_model.php on line 18 
 
Posted: 16 June 2011 01:18 PM   [ # 13 ]   [ Rating: 0 ]
Avatar
Joined: 2011-03-08
417 posts

Did you check your error log?

If there are no errors to be fixed in your error log then I would try copying the example from “Creating your own Models” and change the values where required.

http://ellislab.com/codeigniter/user-guide/general/models.html

<?php

class Blogmodel extends CI_Model {

    
var $title   '';
    var 
$content '';
    var 
$date    '';

    function 
__construct()
    
{
        
// Call the Model constructor
        
parent::__construct();
    
}
    
    
function get_last_ten_entries()
    
{
      error_reporting
(-1);
      
ini_set('display_errors'1);

      
$this->load->database();

      
$query $this->db->get(' users'10);
      
      return 
$query->result();
    
}//

}// 

 
 

 Signature 

Ongoing project:
  http://johns-jokes.com/joke-of-the-day/2010/May.html
My Hippy Trail:
  http://the-road-to-kathmandu.johns-jokes.com/
In case you forget:
  http://deformedweb.co.uk/php_variable_tests.php

 
Posted: 17 June 2011 01:10 AM   [ # 14 ]   [ Rating: 0 ]
Avatar
Joined: 2009-06-19
6583 posts

Show your code were your loading your model!

InsiteFX

 Signature 

Certified State of CT Computer Programming Teacher.
Custom Designed Icons, eBook Covers Software Boxes. CD, DVD Etc. New iPhone® Tab Bar Icons and iPhone® Applications Icons.

Skype: insitfx

STOP! Before posting your questions, remember the WWW Golden rule:
What did you try? What did you get? What did you expect to get?

Input -> Controller | Processing -> Model | Output -> View