2017-02-21 55 views
-1

我使用Codeigniter活动记录一段时间了,这是我得到的最荒谬的错误。

我的查询是

$this->db->select('*'); 
$this->db->from('my_table'); 
$this->db->where('is_active', 1); 

$query = $this->db->get(); 

if ($query->num_rows() > 0) { 
    //return $query->result(); 
    return $query; 
} else { 
    return FALSE; 
} 

当我使用$query->result();这是空的。当我使用return $query; 结果是类似下面,

CI_DB_pdo_result Object 
(
    [num_rows] => 21 
    [conn_id] => PDO Object 
     (
     ) 

    [result_id] => PDOStatement Object 
     (
      [queryString] => SELECT * FROM my_table WHERE is_active = 1 
     ) 

    [result_array] => Array 
     (
     ) 

    [result_object] => Array 
     (
     ) 

    [custom_result_object] => Array 
     (
     ) 

    [current_row] => 0 
    [row_data] => 
) 

计数是

[NUM_ROWS] => 21

这到底是怎么丢失/问题?

回答

0

试试这个

$this->db->select('*'); 
$this->db->from('my_table'); 
$this->db->where('is_active', 1); 

$query = $this->db->get(); 
$result = $query->result_array(); 

if (count($result) > 0) { 
    return $result; 
} 
else{ 
    return FALSE; 
} 

你不是格式化查询阵列。所以格式化它。

0

简单加
$ query = $ this-> db-> get() - > result(); 在你的代码

$this->db->select('*'); 
$this->db->from('my_table'); 
$this->db->where('is_active', 1); 

$query = $this->db->get()->result(); 

if ($query->num_rows() > 0) { 
//return $query->result(); 
     return $query; 
} else { 
return FALSE; 

}

+0

尝试了所有可能的方式包括这也...不...运气 –

+0

$ this-> db-> select('*'); $ this-> db-> from('my_table'); $ this-> db-> where('is_active',1); return $ this-> db-> get() - > result(); 试试这个 –

0

if ($query->num_rows() > 0)然后返回$query->result()它返回object结果format.Like这个..

if ($query->num_rows() > 0) { 
    return $query->result(); 
} else { 
    return FALSE; 
} 

然后使用arrow(->)运算符来检索所需的值。 欲了解更多信息,请点击这里Codeigniter Result Formats

+0

试过了。但结果相同。返回并清空数组。 –

+0

print_r($ query-> result_array())'输出什么? –

+0

打印一个空数组。 'Array ( )' –

1

我发现了这个问题。发生这种情况是因为我的应用程序使用PDO DB Driver。不是MySQL驱动程序。

$db['default']['dbdriver'] = 'pdo'; 

所以我改变了我的模型来支持。

$sql = "SELECT * FROM my_table WHERE is_active = 1"; 
$stmt1 = $this->db->conn_id->prepare($sql); 
$stmt1->execute(); 
return $stmt1->fetchAll(PDO::FETCH_ASSOC); 

现在它工作正常。

我真的很抱歉你的时间的人。并非常感谢 试图帮助。

0

如果你想获得结果,你必须使用row()或result()函数。

$this->db->select('*'); 
$this->db->from('my_table'); 
$this->db->where('is_active', 1); 

$query = $this->db->get(); 

if ($query->num_rows() > 0) { 
    //return $query->result(); 
    return $this->db->get()->result(); 
    //return $query; 
} else { 
    return FALSE; 
} 
0
 $this->db->select('*'); 
$this->db->from('my_table'); 
$this->db->where('is_active', 1); 

$query = $this->db->get(); 

$i = 0; 
foreach ($query->result() as $row) { 
    $i++; 
    $data['id'] = $row->id; 
    $data['username'] = $row->username; 
} 


if ($i > 0) { 
    return $data; 
}