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

标签 php mysql database codeigniter

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

例如,在我的 Controller 中我有 (CONTROLLER)

Name_Model::get_all_from_another_table();

在我的 name_model.php(模型)中

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

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

以上代码返回:

Fatal error: Using $this when not in object context

最佳答案

先试试。

Controller :

 $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;
}

关于php - CodeIgniter 上没有模型对象的数据库查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44080407/

相关文章:

php - 构建可伸缩(可扩展)、可维护和松耦合软件的最佳技术是什么?

PHP、MySQL 从多个表中按关键字搜索

php - WordPress .htaccess RewriteRule 不工作

mysql - 内部联接后仅返回重复记录

database - 如何将 R 列表对象保存到数据库?

mysql - 使用jsp插入mysql数据库抛出异常

php - MySQL查询唯一用户和最小分值

php - MYSQL动态交叉表问题

mysql - 跨 3 个表聚合列

MySQL 每天计数类型