gpt4 book ai didi

mysql - Codeigniter 中的全局 MySQL 查询

转载 作者:行者123 更新时间:2023-11-30 23:16:46 25 4
gpt4 key购买 nike

为了尝试缩小查询范围,我对 Codeigniter 框架还是很陌生...

我想定义全局变量(例如,在自动加载的助手中)并在我的整个站点中使用全局 mysql 查询 - 但我不明白如何执行后者(全局 mysql 查询)。

我理解在助手中定义单个变量的概念...我理解在模型中创建单个 mysql 查询、将其加载到 Controller 中并在 View 文件中使用它(使用 foreach 循环)的概念.

我如何(以及在​​哪里)创建一个可以自动加载(或其他)并在我网站的任何地方使用的 mysql 查询 - 而无需在每个 Controller 中加载它?

最佳答案

这可能会有所帮助.. 模型可能是您最好的选择。下面我给出了一些关于如何使用 controlrs/models/libraries 的一般情况,我会将我的 mysql 代码放入适当的模型文件中。并通过任何 Controller 调用它

//库/profiles.php

class My_library 
{
protected $CI;

public function __construct()
{
$this->CI =& get_instance(); // Existing Code Igniter Instance
}

public function my_lib_method()
{
// Your Code Here
// can communicate back with CI by using $this->CI
// $this->CI->load->view(....);
// $this->CI->load->model(...);
// ETC
}
}

//模型/my_model.php

class My_model extends CI_Model{

public function my_mdl_method(){
// Your Code Here
}

}

// Controller /my_controller.php

class My_controller extends CI_Controller 
{
public function my_ctrl_method(){
$this->load->library('my_library');
$this->load->model('my_model');

// calling a library method
$this->my_library->my_lib_method();

// calling a model method
$this->my_model->my_mdl_method();
}
}

关于mysql - Codeigniter 中的全局 MySQL 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17319092/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com