foreach循环中的codeigniter中有500个内部服务器错误和未定义的变量错误?

时间:2017-11-07 14:59:31

标签: mysql foreach codeigniter-3

我在谷歌搜索了很多,但没有找到任何解决我的问题的方法。最后希望得到解决方案。我用htaccess从url中删除了index.php。 我的htaccess是:

RewriteEngine on
RewriteCond $1 !^(index\.php|public|\.txt) 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1

我的问题出现在我的整个codeigniter项目的任何视图中,如果我想从数据库中获取数据,它会给我一个未定义变量的错误。在chrome调试器中,它显示500 Internal Server Error。 我用于获取数据的变量显示此变量未定义并且还显示505内部错误。我正在共享我的mvc代码。请查看我的代码我正在使用版本3.

型号:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Pay_slips extends CI_Model{

function __construct()  
      {       
        parent::__construct();  
      } 


      function select() {
        $query = $this->db->query('SELECT * FROM india_salary_slip_details');
         return $query;
        } 

}

控制器:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Pay_slips extends CI_Controller {

    function __construct() {
        parent::__construct();        
        $this->load->model('pay_slips');
    }



    public function index()  
      {  
         //load the database  
         $this->load->database();  
         //load the model  
         $this->load->model('select');  
         //load the method of model  
         $data['h']=$this->select->select();  
         //return the data in view  
         $this->load->view('responsibilities/pay_slips', $data);  
      }   


}

查看:

   <?php  
     foreach ($h->result() as $row)
       {  
        ?><tr>  
           <td><?php echo $row->PAY_MONTH;?></td>  
           <td><?php echo $row->PAY_YEAR;?></td>  
          </tr>  
      <?php }  ?> 

有错误的图片: enter image description here

我在数据库中有数据.Eveything很好,但仍然不知道为什么会这样。

2 个答案:

答案 0 :(得分:0)

在你的控制器中用这个替换$ query:

    $query=$this->db->select('*')->from('india_salary_slip_details')->get();

在模型中加载数据库。 然后在控制器中尝试var_dump($ data)。你会发现什么是错的。

答案 1 :(得分:0)

MODEL
=====
<?php if(!defined('BASEPATH')) exit('No direct script access allowed');
class Pay_slips extends CI_Model{

function __construct()  
      {       
        parent::__construct();  
      } 


      function get_salary() {
        $query = $this->db->query('SELECT * FROM india_salary_slip_details');
         return $query->result_array();
        } 

}

CONTROLLER
==========
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Pay_slips extends CI_Controller {

    function __construct() {
        parent::__construct();        
        $this->load->model('pay_slips','slips');
    }

    public function index()  
      {  
         //load the database  
         $this->load->database();  
         //load the model  
         //$this->load->model('select');  
         //load the method of model  
         $data['h']=$this->slips->select();  
         //return the data in view  
         $this->load->view('responsibilities/pay_slips', $data);  
      }   
}

VIEW
====
<?php  
     foreach ($h as $row)
     {  
        ?><tr>  
           <td><?php echo $row['PAY_MONTH'];?></td>  
           <td><?php echo $row['PAY_YEAR'];?></td>  
          </tr>  
<?php }  ?>