Welcome Guest, Not a member yet? Register   Sign In
Fatal error: Call to a member function query() on a non-object
#1

[eluser]Creox[/eluser]
Hi Guys,

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

Code:
$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

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

But when I do a simple call like this:

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

I get the following error:

Quote: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
#2

[eluser]danmontgomery[/eluser]
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/codeignit....php#cl-47
#3

[eluser]Creox[/eluser]
[quote author="noctrum" date="1308237172"]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/codeignit....php#cl-47[/quote]

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.
#4

[eluser]Creox[/eluser]
Anyone?
#5

[eluser]John_Betong_002[/eluser]
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

Code:
// 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();
    

// ***********************************
&nbsp;
&nbsp;
&nbsp;
#6

[eluser]Creox[/eluser]
Thanks,

I know get the error

Code:
Invalid query: Whole query: Resource 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!
#7

[eluser]InsiteFX[/eluser]
You should always check to make sure you retrive data from a query!
Code:
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
#8

[eluser]John_Betong_002[/eluser]
[quote author="Creox" date="1308249769"]Thanks,

I know get the error

Code:
Invalid query: Whole query: Resource 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![/quote]

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.
&nbsp;
&nbsp;
&nbsp;
#9

[eluser]Creox[/eluser]
No problem.

Sorry Guys I still gaent solved this.

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

[eluser]John_Betong_002[/eluser]
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:

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

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

&nbsp;
&nbsp;




Theme © iAndrew 2016 - Forum software by © MyBB