【问题标题】:can't show my database in codeignaiter无法在 codeigniter 中显示我的数据库
【发布时间】:2015-04-17 18:20:26
【问题描述】:

有人可以帮忙吗? 我认为我的代码没有问题。但我仍然无法在 codeigniter 上显示我的数据库。

这是我的代码:

查看:

<html>

<head>
  <title>View Data</title>
</head>

<body>
  <h1>View Data</h1>
  <table border="0">
    <tr>
      <th>Id Buku</th>
      <th>Judul Buku</th>
      <th>Tahun Terbit</th>
    </tr>

    <?php if (is_array($coba)) foreach ($coba as $item) { ?>
    <tr>
      <td>
        <?php echo $item->id_buku;?></td>
      <td>
        <?php echo $item->judul_buku;?></td>
      <td>
        <?php echo $item->tahun_terbit;?></td>
    </tr>
    <?php } ?>

  </table>
</body>

</html>

型号:

<?php 

class Test_model extends CI_Model{

 //konstruktor; dipanggil saat pertama kali object dibuat
 function __construct(){

  parent::__construct();

  /*
   load database;
   database yang di load adalah databse yang kita definisikan pada file database.php
  */
  $this->load->library('table');
  $this->load->database();
 }

 //fungsi yang akan mengambil data pada database
 function get_all_book_data(){

  //query data
  $query = $this->db->get('tabel_coba');


 }
}
 ?>

控制器:

<?php
class Test_controller extends CI_Controller{

 /*
  konstruktor;
  dipanggil saat pertama kali object controller dibuat
  load model
 */
 public function __construct(){
  parent::__construct();
  $this->load->model('test_model');
 }

 /*
  public method;
  load view, tampilkan data pada class view
 */
 public function get_book_data(){

  $data['coba'] = $this->test_model->get_all_book_data();

  $this->load->view('test_view', $data);
 }
}
?>

仅供参考: 它是我的数据库:(数据库名称:留言簿)

【问题讨论】:

    标签: php database codeigniter model-view-controller


    【解决方案1】:

    首先确保您的数据库自动加载 config/autoload.php 并在config/database.php 中设置正确的详细信息

    您可以在用户指南http://www.codeigniter.com/userguide2/http://www.codeigniter.com/userguide2/database/index.html 中找到所有信息

    第二次更改获取图书数据到索引

    class Test_controller extends CI_Controller{
    
    public function __construct(){
        parent::__construct();
        $this->load->model('test_model');
    }
    
    public function index(){
        // Checks if any in array
        $data['coba'] = array();
    
        // Gets data
    
        $results = $this->test_model->get_all_book_data(); 
    
        // Run get results in loop
        foreach ($results as $result) {
           $data['coba'][] = array(
            'id_buku' => $result['id_buku'],
            'judul_buku' => $result['judul_buku'],
            'tahun_terbit' => $result['tahun_terbit']
           );
        }
    
        $this->load->view('test_view', $data);
    }
    
    }
    

    模型中第三个使用if语句判断真假;

    并删除$this-&gt;load-&gt;library('table');$this-&gt;load-&gt;database(); 当您在config/database.php 中设置连接后,您应该自动加载您的数据库

    class Test_model extends CI_Model {
    
        public function get_all_book_data() {
            // Change the tablename to what your table is.
            $query = $this->db->get('tablename');
    
    
            // Checks if there are any items greater than 0 meaning no data
            if ($query->num_rows() > 0) {
                return $query->result_array();
            } else {
                return false;
            }
        }
    }
    

    在视图中试试这个

    <table>
    <thead>
    <tr>
    <th></th>
    <th></th>
    <th></th>
    </tr>
    </thead>
    <tbody>
    <?php if ($coba) { ?>
    <?php foreach ($coba as $item) {?> 
        <tr>
        <td><?php echo $item['id_buku'];?></td>
        <td><?php echo $item['judul_buku'];?></td>
        <td><?php echo $item['tahun_terbit'];?></td>
        </tr>
    <?php } ?>
    <?php } else { ?>
    <tr>
    <td class="text-center" colspan="3">No Results</td>
    </tr>
    <?php } ?>
    <tbody>
    </table>
    

    【讨论】:

      【解决方案2】:

      从模型中移除

       $this->load->library('table');
        $this->load->database();
      

      并放置在您的控制器中。

      然后使用:

      <?php foreach ($coba->result_array() as $item) { ?>
          <tr>
          <td>
          <?php echo $item['id_buku'];?></td>
          <td>
          <?php echo $item['judul_buku'];?></td>
          <td>
          <?php echo $item['tahun_terbit'];?></td>
          </tr>
      <?php } ?>
      

      对不起,我忘了添加你的模型功能...

       function get_all_book_data(){
      
        //query data
        $query = $this->db->get('tabel_coba');
        return $query;
      
       }
      

      【讨论】:

      • 我不会使用这个 $this->load->database();应该自动加载。
      【解决方案3】:

      我看到您的模型 get_all_book_data() 在查询 $query = $this-&gt;db-&gt;get('tabel_coba') 后没有返回任何内容 并检查您的数据库配置是否设置正确。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-03-19
        相关资源
        最近更新 更多