php - 无法将数据从数据库显示到表

标签 php mysql codeigniter

这是我面临的错误:

A PHP Error was encountered
Severity: Notice
Message: Undefined variable: fetch_data
Filename: pages/instructors.php 
Line Number: 126"

这是我的 coach.php 代码行:

          <tr>
            <th>ID</th>
            <th>Last Name</th>
            <th>First Name</th>
            <th>Action</th>
          </tr>
        </thead>
        <tbody>

        <?php
          if($fetch_data) {

            foreach ($fetch_data->result() as $row) {

       ?>
          <tr>  
           <td><?php echo $row->ins_id; ?></td>
           <td><?php echo $row->lastname; ?></td>
           <td><?php echo $row->firstname; ?></td>
           <td><a class="btn btn-info" href="<?php echo base_url('Edit_faculty'); ?>">Edit</a></td> 

          </tr>  
        <?php
            }
          }
          else
          {
        ?>
          <tr>
            <td colspan="3">No data found</td>
          </tr>

       <?php        
        }
       ?>  

        </tbody>
      </table>

我的 Controller :

class Add_instructor extends CI_Controller{

    public function instructor(){
        $this->load->model('instructor_model');
        $data["fetch_data"] = $this->instructor_model->fetch_data();
        $this->load->vars($data);
        $this->load->view('templates/header');
        $this->load->view('pages/add_instructor', $data);
        $this->load->view('templates/footer');
    }
}

还有我的模型:

class instructor_model extends CI_Model {

function fetch_data(){

        $query = $this->db->query("SELECT * FROM instructor ORDER BY ins_id DESC");
        return $query;

    }
}

最佳答案

如下更改您的 Controller 代码。它会起作用的。 Controller 代码

class Add_instructor extends CI_Controller{
    public function instructor(){
        $this->load->model('instructor_model');
        $data["fetch_data"] = $this->instructor_model->fetch_data();
        $this->load->view('templates/header');
        $this->load->view('pages/instructor', $data);
        $this->load->view('templates/footer');
    }
}

型号代码:-

class instructor_model extends CI_Model {
    function fetch_data(){
        $query = $this->db->query("SELECT * FROM instructor ORDER BY ins_id DESC");
    return $query->result();
    }
}

查看代码:-

  <tr>
        <th>ID</th>
        <th>Last Name</th>
        <th>First Name</th>
        <th>Action</th>
      </tr>
    </thead>
    <tbody>

    <?php
      if($fetch_data) {
      foreach($fetch_data as $row) {
     ?>
      <tr>  
       <td><?php echo $row->ins_id; ?></td>
       <td><?php echo $row->lastname; ?></td>
       <td><?php echo $row->firstname; ?></td>
       <td><a class="btn btn-info" href="<?php echo base_url('Edit_faculty'); ?>">Edit</a></td> 

      </tr>  
    <?php
        }
      }
      else
      {
    ?>
      <tr>
        <td colspan="3">No data found</td>
      </tr>

   <?php        
    }
   ?>  

    </tbody>
  </table>

关于php - 无法将数据从数据库显示到表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48358479/

相关文章:

php - CodeIgniter - radio 输入上的 PHP Set_Radio

php - Mysql group_concat 查询出现错误

php - Mysql - PHP 查询

javascript - 如何将参数 lang 传递给另一个 url

php - 尝试在数据库中搜索时显示重复选项

php - 如何在 PDO 类中添加 MySQL 事务?

mysql - 如何查找mysql数据库的最后更新日期

php - 将多个日期添加到 Wordpress 中的自定义帖子类型

php - 比较提交与数据库的值(value)

php - 更新 mysql 表中的特定行