2017-04-03 192 views
1

我正在使用最新的CodeIgniter版本(3.1.4)。我正在運行以下基本查詢:Codeigniter查詢返回空白

$query = $this->db->query("SELECT * FROM mytable;"); 

並獲得空白結果 - num_rows和row_data都爲空。這裏是$查詢的print_r輸出:

CI_DB_mysqli_result Object 
(
    [conn_id] => mysqli Object 
     (
      [affected_rows] => 2 
      [client_info] => mysqlnd 5.0.12-dev - 20150407 - $Id: b5c5906d452ec590732a93b051f3827e02749b83 $ 
      [client_version] => 50012 
      [connect_errno] => 0 
      [connect_error] => 
      [errno] => 0 
      [error] => 
      [error_list] => Array 
      (
      ) 

      [field_count] => 7 
      [host_info] => Localhost via UNIX socket 
      [info] => 
      [insert_id] => 0 
      [server_info] => 5.6.35 
      [server_version] => 50635 
      [stat] => Uptime: 2988249 Threads: 2 Questions: 13445030 Slow queries: 9 Opens: 784 Flush tables: 1 Open tables: 469 Queries per second avg: 4.499 
      [sqlstate] => 00000 
      [protocol_version] => 10 
      [thread_id] => 922470 
      [warning_count] => 0 
     ) 

    [result_id] => mysqli_result Object 
    (
     [current_field] => 0 
     [field_count] => 7 
     [lengths] => 
     [num_rows] => 2 
     [type] => 0 
    ) 

    [result_array] => Array 
    (
    ) 

    [result_object] => Array 
    (
    ) 

    [custom_result_object] => Array 
    (
    ) 

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

任何人都可以幫我弄清楚這裏有什麼問題嗎?

回答

3

這些值將保持空白,直到您調用其中一種生成結果的方法 - documented here

試試這個

$query = $this->db->query("SELECT * FROM mytable;"); 
echo "Rows: ". $query->num_rows(). "<br>"; 
$data = $query->result(); 
var_dump($data); 

看到什麼?

+0

是的,現在開始工作,謝謝。我認爲這是因爲在更新版本的CI中從mysql遷移到mysqli?在此之前使用2.x版本,並從未遇到過問題。 –

0

我希望這種變化將幫助您

$query = $this->db->query("SELECT * FROM mytable;"); 
$data = $query->result(); 
return $data; 

你可以重寫以其他方式

$this->db->select('*'); 
$this->db->from('mytable'); 
$data=$this->db->get(); 

數據導致

return $data->result(); 

的行數此查詢

return $data->num_rows();