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 Pwm双极模式H桥驱动控制电机
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题