drr25281 2016-07-28 21:36
浏览 40
已采纳

在CodeIgniter中的项目中遇到php错误

I want to set up my first project in CodeIgniter which includes a database. I've made the proper configurations for database like shown in this tutorial: Everything You Need to Get Started With CodeIgniter.

I have created my database in phpMyadmin. When try to access the project in my localhost this is the error: enter image description here After changing password from 'root' to ' ' now the error is this:

Fatal error: Class 'Model' not found in C:\xampp\htdocs\ci\application\models\diploma_model.php on line 2

This is my model cpde:

<?php
class diploma_model extends Model {
 
    function diploma_model()
    {
        // Call the Model constructor
        parent::Model();
    }
     
    function getData()

        {
            //Query the data table for every record and row
            $query = $this->db->get('data');
             
            if ($query->num_rows() > 0)
            {
                //show_error('Database is empty!');
            }else{
                return $query->result();
            }
        }
 
}
?>

Im new at codeIgniter. Can someone explain why this error occurs? Thanks!

</div>
  • 写回答

2条回答 默认 最新

  • dongtiaozhou4914 2016-07-28 22:21
    关注

    Your tutorial that you have read is very out of date. The current codeigniter website is here

    It would be worth reading through here

    This is the correct way for CI model and anatomy-of-a-model I think because you just extend Model and no CI_Model

    And your construct is wrong to.

    On autoload.php I would autoload database.

    $autoload['libraries'] = array('database');
    

    Filename Diploma_model.php

    <?php    
    
    class Diploma_model extends CI_Model {
    
    public function __construct() {
    
        // Call the Model constructor
        parent::__construct();
    }
    
    function getData() {
      $query = $this->db->get('data');
    
      // This > means greater. So will return results if any found!
    
      if ($query->num_rows() > 0) {
    
         return $query->result();
    
         //return $query->result_array();
    
       } else {
    
           return false;
    
        }
      }
    
    }
    

    Loading of the model

    <?php 
    
    class Example extends CI_Controller {
    
       public function __construct() {
          parent::__construct();
          $this->load->model('diploma_model');
       }
    
       public function index() {
          $data['lists'] = $this->diploma_model->getData();
    
          $this->load->view('someview', $data);
       }
    
    }
    

    The CI User guide for models

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了