2017-04-01 199 views
1

我不知道为什么从phpMyAdmin 型号我CI返回错误的值:`CI`返回错误值

<?php 

class Post extends CI_Model 
{ 
function __construct() 
{ 
    parent::__construct(); 

} 
function getallpost() 
{ 
    return $this->db->get('post'); 


} 

} 
?> 

而且

控制器:

<?php 

defined('BASEPATH') OR exit('No direct script access allowed'); 

class Postc extends CI_Controller { 
    function index() 
    { 
    $this->load->model('post'); 
    $posts=$this->post->getallpost(); 
    echo '<pre>'; 
    print_r($posts); 

    } 
} 
?> 

enter image description here

回答

1

你的代码不会给你任何错误或者,它正在做你正在要求它做的事情。也许你需要对你想获得的信息有所了解。你在你的模型中看到

function getallpost() 
{ 
return $this->db->get('post'); 
} 

上述函数将返回一个对象。如果你想获得一个数组,你需要写

return $this->db->get('post')->result_array(); 

并确保你有你的职位表中的一些数据打印学习更多有关你应该阅读查询数据库CodeIgniter的Query Builder Class

+0

这将工作。 – kishor10d

+0

你走吧:)。另请阅读https://www.codeigniter.com/user_guide/general/views.html以获取标准惯例以创建视图 –

0

如果你想得到回报你的结果为数组,然后使用

return $this->db->get('post')->result_array(); 

如果你想获得回报您的结果作为对象,然后用

return $this->db->get('post')->result(); 
+0

非常感谢您的帮助 –

1

更改模型代码

<?php 
    class Post extends CI_Model 
    { 
     function __construct() 
     { 
      parent::__construct(); 
     } 
     function getallpost() 
     { 
      $query = $this->db->get('post'); 
      if($query->num_rows() > 0) 
      { 
       return $query->result(); 
      }else{ 
       return false; 
      } 
     } 
    } 
+0

无需在模型关闭或控制器关闭时使用https://www.codeigniter .com/user_guide/general/styleguide.html#php-closing-tag – user4419336

+0

非常感谢 –