Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php CodeIgniter上没有模型对象的数据库查询_Php_Mysql_Database_Codeigniter - Fatal编程技术网

Php CodeIgniter上没有模型对象的数据库查询

Php CodeIgniter上没有模型对象的数据库查询,php,mysql,database,codeigniter,Php,Mysql,Database,Codeigniter,我是否可以在不创建模型的新实例的情况下进行数据库查询 例如,在我的控制器中,我有(控制器) 以我的名字_model.php(model) 这种情况发生在具有预定义值的表在创建模型的新实例之前加载时 上述代码返回: Fatal error: Using $this when not in object context 先试试看 控制器: $this->load->model( 'name_model' ); $data = $this->name_model->get

我是否可以在不创建模型的新实例的情况下进行数据库查询

例如,在我的控制器中,我有(控制器)

以我的名字_model.php(model)

这种情况发生在具有预定义值的表在创建模型的新实例之前加载时

上述代码返回:

Fatal error: Using $this when not in object context
先试试看

控制器:

 $this->load->model( 'name_model' );
 $data = $this->name_model->get_all_from_another_table()->result();
 print_r( $data ); //just for testing...
//Model name: name_model.php

public static function get_all_from_another_table() {
    $this->db->query('select * from another_table');
    return $x;
}
型号:

 $this->load->model( 'name_model' );
 $data = $this->name_model->get_all_from_another_table()->result();
 print_r( $data ); //just for testing...
//Model name: name_model.php

public static function get_all_from_another_table() {
    $this->db->query('select * from another_table');
    return $x;
}
控制器:

 $this->load->model( 'name_model' );
 $data = $this->name_model->get_all_from_another_table()->result();
 print_r( $data ); //just for testing...
//Model name: name_model.php

public static function get_all_from_another_table() {
    $this->db->query('select * from another_table');
    return $x;
}
型号:

 $this->load->model( 'name_model' );
 $data = $this->name_model->get_all_from_another_table()->result();
 print_r( $data ); //just for testing...
//Model name: name_model.php

public static function get_all_from_another_table() {
    $this->db->query('select * from another_table');
    return $x;
}

为什么不试试
$this->load->model('name\u model')
?在控制器上执行此操作。我们应该做
$this->name\u model->get\u all\u from\u other\u table()
调用它。@Ukasyah
致命错误:在不在对象上下文中时使用$this
。同样的错误也会发生。我认为在函数上使用“static”时不能使用$this。我不这样认为,在CI 3中,加载时可以使用小写。相信我,它是有效的。好吧,也许它是有效的,但有时它会影响我们,所以我们必须养成这个好习惯,它对我们是有好处的。