为什么我的 codeigniter 模型无法加载?

发布于 2024-11-25 00:50:08 字数 1792 浏览 1 评论 0原文

这是我的模型:

   <?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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

伴梦长久 2024-12-02 00:50:08
function getAll() {
    parent::Model();   //<-------------------Remove this line
    $this->db->select('title, content');
    $this->db->from('posts');

快速浏览了一下,为什么会有这条线呢?

这是对不再存在的旧版本模型基类的 php4 构造函数调用。

删除并重试。

编辑

此外,您不能将模型和控制器命名为相同的名称,它们存在命名空间冲突。

调用模型 Dbtest_model 或其他名称并以这种方式使用它。

另外,这是不必要的

    if($q->num_rows() > 0) {
        foreach ($q->result() as $row) {
            $data[] = $row;
        }
        return $data;
    }

$q->result() 返回一个数组,不需要循环和重建...只需这样做...

    if($q->num_rows() > 0) {
        return $q->result();
    }
function getAll() {
    parent::Model();   //<-------------------Remove this line
    $this->db->select('title, content');
    $this->db->from('posts');

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

    if($q->num_rows() > 0) {
        foreach ($q->result() as $row) {
            $data[] = $row;
        }
        return $data;
    }

$q->result() returns an array, no need to loop through and rebuild... Just do this...

    if($q->num_rows() > 0) {
        return $q->result();
    }
死开点丶别碍眼 2024-12-02 00:50:08

好吧,在经历了一些相当蹩脚的东西之后......我终于发现问题出在我想要在

class CI_model{}

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

class CI_model{}

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!

深陷 2024-12-02 00:50:08

如果您调用 parent::CI_Model(); 而不是 parent::__construct(); 会发生什么?

Ci 被设计为在 php4 上工作,直到 php5 才添加 __construct()。

What happens if you call parent::CI_Model(); instead of parent::__construct();?

Ci is designed to work on php4, and __construct() wasn't added until php5.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文