无法从 codeigniter 中的控制器页面连接模型页面
Unable to connect model page from controller page in code igniter
这是我的数据库php页面
$active_group = 'default';
$query_builder = TRUE;
$db['default'] = array(
'dsn' => '',
'hostname' => '192.168.1.200',
'username' => 'name',
'password' => 'password',
'database' => 'codeignter_tutorial',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
我无法从控制器页面加载模型页面。
这是错误,我将不胜感激。
致命错误:第 16 行 /var/www/html/bcit-ci-CodeIgniter-3.1.11-0-gb73eb19/bcit-ci-CodeIgniter-b73eb19/application/controllers/Welcome.php 中调用未定义的方法 CI_Loader::models()
遇到 PHP 错误
严重性:错误
消息:调用未定义的方法 CI_Loader::models()
文件名:controllers/Welcome.php
行号:16
回溯:
这是我的控制器页面 Welcome.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function index()
{
$this->load->view('dashboard');
}
public function __construct()
{
parent::__construct();
$this->load->helper(array("form","url"));
$this->load->models('models');
}
public function login()
{
$this->load->view('loginPage');
}
public function dashboard()
{
$data['name']=$this->input->post('name');
$data['age']=$this->input->post('age');
$this->models->dbdata($data);
}
}
?>
这是我的模型页面 models.php
<?php
class models extends CI_model
{
public function dbdata($inserttodb)
{
$this->db->insert("registration",$inserttodb);
return $var->result_array();
}
}
?>
模型页面中模型的'm'应该是大写的
class **M**odels extends CI_model
在控制页面
this->load->model('models');
这是从控制器加载模型的方式:
public function __construct()
{
parent::__construct();
$this->load->model('models', 'your_model'); //use 'your_model' to reference your model
}
你是这样称呼你的模型的:
$this->your_model->dbdata($data);
//'your_model' is from constructor, 'dbdata' is the name of your function in model
然后确保方法接受 'data':
public function dbdata($data)
{
//your code goes here
}
这是我的数据库php页面
$active_group = 'default';
$query_builder = TRUE;
$db['default'] = array(
'dsn' => '',
'hostname' => '192.168.1.200',
'username' => 'name',
'password' => 'password',
'database' => 'codeignter_tutorial',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
我无法从控制器页面加载模型页面。 这是错误,我将不胜感激。
致命错误:第 16 行 /var/www/html/bcit-ci-CodeIgniter-3.1.11-0-gb73eb19/bcit-ci-CodeIgniter-b73eb19/application/controllers/Welcome.php 中调用未定义的方法 CI_Loader::models() 遇到 PHP 错误 严重性:错误
消息:调用未定义的方法 CI_Loader::models() 文件名:controllers/Welcome.php 行号:16 回溯:
这是我的控制器页面 Welcome.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function index()
{
$this->load->view('dashboard');
}
public function __construct()
{
parent::__construct();
$this->load->helper(array("form","url"));
$this->load->models('models');
}
public function login()
{
$this->load->view('loginPage');
}
public function dashboard()
{
$data['name']=$this->input->post('name');
$data['age']=$this->input->post('age');
$this->models->dbdata($data);
}
}
?>
这是我的模型页面 models.php
<?php
class models extends CI_model
{
public function dbdata($inserttodb)
{
$this->db->insert("registration",$inserttodb);
return $var->result_array();
}
}
?>
模型页面中模型的'm'应该是大写的
class **M**odels extends CI_model
在控制页面
this->load->model('models');
这是从控制器加载模型的方式:
public function __construct()
{
parent::__construct();
$this->load->model('models', 'your_model'); //use 'your_model' to reference your model
}
你是这样称呼你的模型的:
$this->your_model->dbdata($data);
//'your_model' is from constructor, 'dbdata' is the name of your function in model
然后确保方法接受 'data':
public function dbdata($data)
{
//your code goes here
}