php Codeigniter 致命错误:在非对象上调用成员函数 query()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12931555/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Codeigniter Fatal error: Call to a member function query() on a non-object
提问by Daniela
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?
我查了一下,用户和传入数据库没问题,数据库加载到conf中,查了一下表名。那么还有什么?
$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);
采纳答案by GBD
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
回答by Muhammad Raheel
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.
还要确保数据库存在并且您有权访问它。
回答by m4t1t0
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');
回答by Venkata Krishna
Hi 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 在他的帖子中解释的语法。但是您说无法访问数据库,这意味着它无法检测到配置文件夹中的数据库、php。因此,将该文件链接放在外部的 php 文件中。那么它可能会起作用。
回答by Sachin
That was the problem. I set $db['default']['autoinit'] = TRUE and it worked.
那就是问题所在。我设置了 $db['default']['autoinit'] = TRUE 并且它起作用了。

