doudong8713 2011-07-20 16:11
浏览 34
已采纳

Code Igniter返回空结果集但num_rows(5)

Situation/Problem

I'm pulling all records form a table. I'm passing the returned data to my view. Num_rows says 5 but the result set is empty although there are records in the table.

controller

public function index()
{
     $this->load->model('Accessmodel', 'access');

     $data['query'] = $this->access->get_access_roles();

     $this->load->view('common/header');
     $this->load->view('admin/access/index', $data);
     $this->load->view('common/footer');
}

model

class Accessmodel extends Cruddy {
    function __construct() 
    {
        parent::__construct();
        self::$table = "EmployeeRoleAccess";
    }

    function get_access_roles()
    {
        return $this->db->get(self::$table);
    }
}

Relevant portion of cruddy

class Cruddy extends CI_Model {

protected static $table;

function __construct()
{

    parent::__construct();

}

function get_items()
{

    $this->db->from(self::$table);

    return $this->db->get();

}

view

<div class="column-left">
    <div class="sub-section-left">
        <?php include_once APPPATH . 'views/admin/menu.php'; ?>
    </div>
    <div class="sub-section-left shadows">
        <?php include_once APPPATH . 'libraries/minical.php'; ?>
    </div>
    </div>
    <div class="column-right">  
    <div class="sub-section-right">
        <h1>Employee Role Access Templates<input class="create right" type="button" name="" value="New Access Role" onClick=""></h1>
        <?php var_dump($query) ?>
    </div>
</div>

var_dump of query

object(CI_DB_mysql_result)#19 (8) {
  ["conn_id"]=>resource(11) of type (mysql link persistent)
  ["result_id"]=>resource(19) of type (mysql result)
  ["result_array"]=>array(0) { }
  ["result_object"]=>array(0) { }
  ["custom_result_object"]=>array(0) { }
  ["current_row"]=>int(0)
  ["num_rows"]=>int(5)
  ["row_data"]=>NULL
}
  • 写回答

1条回答 默认 最新

  • douzoudang1511 2011-08-01 20:32
    关注

    I forgot that result is an object. I was trying to access it like an array

    echo $Result['Name'];
    

    which is why I used var_dump($Result) to see what was in the variable. When I saw result_array was empty I started to think it wasn't returning data... this was NOT the case. I just had not returned data using result_array so OF COURSE IT WAS EMPTY! You can specify which way to return data with codeigniter, the default being through an object.

    Below is an example of the code I used in the view and it does work. I have to admit, I had not had any caffeine when I wrote this code this morning.... my apologies

    if($query->num_rows() > 0) { 
        foreach($query->result() as $Result) {
            echo $Result->Name . "<br />
    ";
        }
    } else { 
        echo "There are no results!";
    } 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab