致命错误:在 CodeIgniter 中使用活动记录时调用成员函数 insert()

发布于 2024-12-12 12:40:08 字数 1599 浏览 0 评论 0原文

可能的重复:
调用非对象上的成员函数
调用非对象上的成员函数

当我尝试使用 CodeIgniters Active 记录访问数据库时,它总是给我错误

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

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

下面是我的数据库中的设置

$active_group = 'default';
$active_record = TRUE;

$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = '';
$db['default']['database'] = 'test';
$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;

,我已自动加载它

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

,下面是我的模型

class Leads_model extends CI_Model {
    
function __construct()
  {
    parent::__construct();
  }
    
    function insertq(){
        
        $q=$this->db->insert('test',$data);
        
        if ($q){
            
            return true;
        }       
        else
        {
            return false;
        }
        
        
    }
}

有人可以建议我我做错了什么以及为什么我会收到该错误吗?

Possible Duplicate:
Call to a member function on a non-object
Call to a member function on a non-object

When i try to access the database using CodeIgniters Active records it always gives me the error

Fatal error: Call to a member function insert() on a non-object

Fatal error: Call to a member function get() on a non-object

below is the settings in my database

$active_group = 'default';
$active_record = TRUE;

$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = '';
$db['default']['database'] = 'test';
$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;

and i have auto-loaded it

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

and below is my model

class Leads_model extends CI_Model {
    
function __construct()
  {
    parent::__construct();
  }
    
    function insertq(){
        
        $q=$this->db->insert('test',$data);
        
        if ($q){
            
            return true;
        }       
        else
        {
            return false;
        }
        
        
    }
}

can someone please suggest me what am i doing wrong and why am i getting that error?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

锦上情书 2024-12-19 12:40:08

您正在调用父构造函数吗?

class Leads_model extends CI_Model 
{
    function __construct()
    {
        parent::__construct();
    }
   ...
}

Are you calling the parent constructor?

class Leads_model extends CI_Model 
{
    function __construct()
    {
        parent::__construct();
    }
   ...
}
沫雨熙 2024-12-19 12:40:08

确实很奇怪。
确保:
1)您的错误涉及该模型,而不是另一个模型;
2) 在模型中调用 __construct()

如果这两项检查都通过了,请尝试及时加载并查看问题是否仍然存在:

class Leads_model extends CI_Model {

  function __construct()
  {
    parent::__construct();
  }

  function test()
  {
    $this->load->database();
    $data = array('field1' => 'value1','field2' => 'value2');
    $this->db->insert('test',$data);
  }

  function run_test()
  {
     $dbo = $this->load->database('default',TRUE);
     $query = $this->$dbo->get('tablename');
     echo $query->num_rows();
  }

}

您还可以查看函数是否返回连接 ID,作为测试:

  $dbobject = $this->load->database('default',TRUE);
  var_dump($dbobject->conn_id);

It's pretty strange indeed.
Make sure:
1) your error refers to that model, and not to another one;
2) you call the __construct() in your Model

If both of this checks are passed, try in-time loading and see if the problem persists:

class Leads_model extends CI_Model {

  function __construct()
  {
    parent::__construct();
  }

  function test()
  {
    $this->load->database();
    $data = array('field1' => 'value1','field2' => 'value2');
    $this->db->insert('test',$data);
  }

  function run_test()
  {
     $dbo = $this->load->database('default',TRUE);
     $query = $this->$dbo->get('tablename');
     echo $query->num_rows();
  }

}

You can also see if the functions returns the connection id, as a test:

  $dbobject = $this->load->database('default',TRUE);
  var_dump($dbobject->conn_id);
只涨不跌 2024-12-19 12:40:08

问题是我添加了一些变量来将自动完成工作放入模型核心中。这就是导致问题的原因,并且在删除这些问题后它仍然可以工作。

但自动完成现在似乎不起作用。

The problem was i had added some variables to get the autocomplete work in to the model core. That was causing the problem and its working after removing those.

but the auto complete doesn't seem to work now.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文