Codeigniter致命错误:在非对象上调用成员函数query()

时间:2022-10-06 13:50:06

Now I am learning Codeigniter. I have more databases, so I choosed this time Anketa one. Why is error :

现在我正在学习Codeigniter。我有更多的数据库,所以我这次选择了Anketa。为什么是错误:

**Fatal error: Call to a member function query() on a non-object in /var/www/domains/svastara/application/controllers/anketa.php on line 12** ???

I chacked, the user and pass in database is ok, database is loaded in conf, and chacked the table name. So what more?

我chacked,用户和传入数据库是好的,数据库是在conf中加载,并且chacked表名。那还有什么?

$this->db = $this->load->database('anketa');

        $q = $this->db->query("SELECT * FROM anketaip");

        if($q->num_rows()>0){
        foreach ($q->result() as $row)
        {
            $data[] = $row;
        }

        }return $data;

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

5 个解决方案

#1


2  

Below line doesn't return you database object until you pass second argument as TRUE

在将第二个参数传递为TRUE之前,下面的行不会返回数据库对象

Change:

更改:

$this->db = $this->load->database('anketa');

To

$newdb = $this->load->database('anketa',TRUE);

$q = $newdb->query("SELECT * FROM anketaip");

Reference Link: http://codeigniter.com/user_guide/database/connecting.html

参考链接:http://codeigniter.com/user_guide/database/connecting.html

#2


3  

You dont have to do this

你不必这样做

$this->db = $this->load->database('anketa');

You can simply do it like this

你可以这样做

$this->load->database('anketa');

$this->db->query('blah blah');

Also make sure database exists and you have rights to access it.

还要确保数据库存在,并且您有权访问它。

#3


1  

Posibly you need to load the DB library, please check in you autoload.php that you are loading the database library

您可能需要加载数据库库,请在autoload.php中检查您正在加载数据库库

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

Or load the library in your controller

或者在控制器中加载库

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

#4


0  

Hi Daniela,

嗨Daniela,

I think this resource link may help u. Because when you are using more than one database at a time u need to follow the syntax explained by GDB in his post. But you said unable to access to database that means it's not able to detect database,php in config folder. So put that file link externally in your php file. Then it may work.

我认为这个资源链接可能对你有帮助。因为当您一次使用多个数据库时,您需要在他的帖子中遵循GDB解释的语法。但是你说无法访问数据库,这意味着它无法在config文件夹中检测数据库,php。所以把那个文件链接放在你的php文件中。然后它可能会工作。

http://codeigniter.com/forums/viewthread/191673/

http://codeigniter.com/forums/viewthread/191673/

#5


0  

That was the problem. I set $db['default']['autoinit'] = TRUE and it worked.

那就是问题所在。我设置$ db ['default'] ['autoinit'] = TRUE并且它有效。

#1


2  

Below line doesn't return you database object until you pass second argument as TRUE

在将第二个参数传递为TRUE之前,下面的行不会返回数据库对象

Change:

更改:

$this->db = $this->load->database('anketa');

To

$newdb = $this->load->database('anketa',TRUE);

$q = $newdb->query("SELECT * FROM anketaip");

Reference Link: http://codeigniter.com/user_guide/database/connecting.html

参考链接:http://codeigniter.com/user_guide/database/connecting.html

#2


3  

You dont have to do this

你不必这样做

$this->db = $this->load->database('anketa');

You can simply do it like this

你可以这样做

$this->load->database('anketa');

$this->db->query('blah blah');

Also make sure database exists and you have rights to access it.

还要确保数据库存在,并且您有权访问它。

#3


1  

Posibly you need to load the DB library, please check in you autoload.php that you are loading the database library

您可能需要加载数据库库,请在autoload.php中检查您正在加载数据库库

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

Or load the library in your controller

或者在控制器中加载库

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

#4


0  

Hi Daniela,

嗨Daniela,

I think this resource link may help u. Because when you are using more than one database at a time u need to follow the syntax explained by GDB in his post. But you said unable to access to database that means it's not able to detect database,php in config folder. So put that file link externally in your php file. Then it may work.

我认为这个资源链接可能对你有帮助。因为当您一次使用多个数据库时,您需要在他的帖子中遵循GDB解释的语法。但是你说无法访问数据库,这意味着它无法在config文件夹中检测数据库,php。所以把那个文件链接放在你的php文件中。然后它可能会工作。

http://codeigniter.com/forums/viewthread/191673/

http://codeigniter.com/forums/viewthread/191673/

#5


0  

That was the problem. I set $db['default']['autoinit'] = TRUE and it worked.

那就是问题所在。我设置$ db ['default'] ['autoinit'] = TRUE并且它有效。