为什么我的 codeigniter 模型无法加载?
这是我的模型:
<?php
class Dbtest extends CI_Model {
function __construct()
{
// Call the Model constructor
parent::__construct();
}
function getAll() {
parent::Model();
$this->db->select('title, content');
$this->db->from('posts');
$this->db->where('post_id', 1);
$q = $this->db->get();
if($q->num_rows() > 0) {
foreach ($q->result() as $row) {
$data[] = $row;
}
return $data;
}
}
}
这是我的控制器:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Dbtest extends CI_Controller {
function index() {
$this->load->model('dbtest');
$data['rows'] = $this->dbtest->getAll();
$this->load->view('dbtest', $data);
}
}
这是我的视图,
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<div id="container">
<p>My view has been loaded</p>
<?php foreach($rows as $r) : ?>
<h1><?php echo $r->title; ?></h1>
<div><?php echo $r->content; ?></div>
<?php endforeach; ?>
</div>
</body>
</html>
我不断收到 500 服务器错误。我可能做错了什么。我已经自动加载了数据库。请帮助一位尝试学习这个优秀框架的 Codeigniter 新手。
Here's my model:
<?php
class Dbtest extends CI_Model {
function __construct()
{
// Call the Model constructor
parent::__construct();
}
function getAll() {
parent::Model();
$this->db->select('title, content');
$this->db->from('posts');
$this->db->where('post_id', 1);
$q = $this->db->get();
if($q->num_rows() > 0) {
foreach ($q->result() as $row) {
$data[] = $row;
}
return $data;
}
}
}
Here's my controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Dbtest extends CI_Controller {
function index() {
$this->load->model('dbtest');
$data['rows'] = $this->dbtest->getAll();
$this->load->view('dbtest', $data);
}
}
And here's my view
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<div id="container">
<p>My view has been loaded</p>
<?php foreach($rows as $r) : ?>
<h1><?php echo $r->title; ?></h1>
<div><?php echo $r->content; ?></div>
<?php endforeach; ?>
</div>
</body>
</html>
I keep getting a 500 server error. What could I possibly be doing wrong. I have autoloaded the database library. Please help a codeigniter newbie trying to learn this great framework.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
快速浏览了一下,为什么会有这条线呢?
这是对不再存在的旧版本模型基类的 php4 构造函数调用。
删除并重试。
编辑
此外,您不能将模型和控制器命名为相同的名称,它们存在命名空间冲突。
调用模型 Dbtest_model 或其他名称并以这种方式使用它。
另外,这是不必要的
$q->result()
返回一个数组,不需要循环和重建...只需这样做...After taking a quick look, why is this line there?
It is a php4 constructor call to the old version of the Model base class that no longer exists..
remove and try again.
EDIT
Also, you cannot name a model AND Controller the same name, they have namespace conflicts.
call the model
Dbtest_model
or something and use it that way.Also, this is unnecessary
$q->result()
returns an array, no need to loop through and rebuild... Just do this...好吧,在经历了一些相当蹩脚的东西之后......我终于发现问题出在我想要在
CI 应用程序的核心模型(应用程序/系统/核心/模型)之后放置一些变量,只是为了自动完成Eclipse/aptana/netbeans。因此,如果您可以从干净的 CI 安装恢复正常,我认为它应该恢复正常,这是我的最终解决方案。快乐编码!
Well after some pretty crappy stuff... I finally found that the problem was in my desition to put some Vars after the
in the Core Model (application/system/core/Model) of my CI application, just for the goods of autocompletion for Eclipse/aptana/netbeans. So if you can revert back to normal from a clean CI installation, i think it should get back to normal, this was my final solution. Happy Coding!
如果您调用
parent::CI_Model();
而不是parent::__construct();
会发生什么?Ci 被设计为在 php4 上工作,直到 php5 才添加 __construct()。
What happens if you call
parent::CI_Model();
instead ofparent::__construct();
?Ci is designed to work on php4, and
__construct()
wasn't added until php5.