控制器
class Main extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model("user_model");
}
public function login()
{
/* Display the login page */
$this->load->view("form");
$this->user_model->get_all_users;
}
public function validate()
{
/* Validate the login details passed via POST parameter.
If successful, create session and redirect to people list page.
If failure, redirect to login page. */
if (isset($_POST["name"], $_POST["password"])) {
$name = $_POST["name"];
$password = $_POST["password"];
}
模型
class user_model extends CI_Model {
public function __construct() {
parent::__construct();
$this->load->database();
}
public function get_all_users() {
$res = $this->db->from('account');
if ($res) {
return $res->result_array();
}
return null;
}
public function get_user($id) {
$res = $this->db->from("account")->where("id", $id)->get();
if ($res) {
return $res->result_array();
}
return null;
}
}
就像我在控制器中使用user_model一样,它也说在模型中找不到result_array。假设我的数据库配置正确。任何人都可以帮我这个吗?
数据库设置
$db['default'] = array(
'dsn' => '',
'hostname' => '127.0.0.1',
'username' => 'root',
'password' => 'correctpassword',
'database' => 'challenge',
'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
);
答案 0 :(得分:0)
你应该给你并给你模型方法。
使用此
$this->user_model->get_all_users();
而不是
$this->user_model->get_all_users;
数据库的每个项目
foreach ($query->result() as $row)
{
echo $row->title;
echo $row->name;
echo $row->body;
}
答案 1 :(得分:0)
在您的模型中使用B
代替class B; // class `B` forward-declaration
// define class A, but don't implement the parts that need B's definition
class A {
public:
B& b_; // `A` can use `B` here, but the compiler still doesn't know how B is defined
A(B&& b); // also need to define A's structure, but not the method implementations
void foo();
};
class B {
public:
A* a_;
B() : {
a_ = new A( *this );
}
void foo() { }
};
// now we can define `A` using the `A::` syntax, instead of class{}:
A::A(B&& b) : b_(b) { }
void A::foo() { b_.foo(); }
int main()
{
B b;
b.a->foo();
return 0;
}
get
答案 2 :(得分:0)
如果您想使用任何CI的帮助程序,模型或其他库,则需要通过CI实例执行此操作。这是通过这样做来实现的:
public function __construct()
{
$this->CI =& get_instance();
}
你应该以这种方式调用你的模型方法。
$这 - > user_model-> GET_ALL_USERS();
并确保您在库中自动加载模型
答案 3 :(得分:0)
通常,如果您在codeignitor中工作以确保您的文件名和类名相同,那么将第一个字母保留为控制器文件和模型文件的大写是很好的做法,因为它可能会在实际运行时产生问题服务器。
对于您的问题,请进行这些更改并检查,如果您仍有错误,请在评论中分享屏幕截图
控制器文件
public function __construct()
{
parent::__construct();
$this->load->model("user_model", "um");
}
模型文件
确保您制作模型文件的首字母和类名
public function get_all_users() {
$res = $this->db->select('*')
->from('account')
->get();
if ($res) {
return $res->result_array();
}
return null;
}