2017-02-21 52 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; 
}