情况/问题
我正在将所有记录提取到一个表中。我将返回的数据传递到我的 View 。 Num_rows 显示为 5,但尽管表中有记录,但结果集为空。
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');
}
型号
class Accessmodel extends Cruddy {
function __construct()
{
parent::__construct();
self::$table = "EmployeeRoleAccess";
}
function get_access_roles()
{
return $this->db->get(self::$table);
}
}
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();
}
查看
<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
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
}
最佳答案
我忘记了 result 是一个对象。我试图像数组一样访问它
echo $Result['Name'];
这就是为什么我使用 var_dump($Result) 来查看变量中的内容。当我看到 result_array 为空时,我开始认为它没有返回数据......事实并非如此。我只是没有使用 result_array 返回数据,所以当然它是空的!您可以使用codeigniter指定返回数据的方式,默认是通过对象。
下面是我在 View 中使用的代码示例,它确实有效。我必须承认,今天早上我写这段代码时没有喝任何咖啡因......我很抱歉
if($query->num_rows() > 0) {
foreach($query->result() as $Result) {
echo $Result->Name . "<br />\n";
}
} else {
echo "There are no results!";
}
关于php - Codeigniter 返回空结果集,但返回 num_rows(5),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6765031/