查詢出來的$query怎麼轉化成data數組?

來源:互聯網
上載者:User
$query=$this->db->select('select dep_id, dep_name from xy_departments order by dep_id asc');


查詢出來的$query怎麼轉化成
$data = array(
'財經' => '財經',
'哲學' => '哲學'
);
這樣的數組形式


回複討論(解決方案)

print_r($query);

看到的是什麼

print_r($query);

看到的是什麼



CI_DB_mysql_result Object
(
[conn_id] => Resource id #51
[result_id] => Resource id #59
[result_array] => Array
(
)

[result_object] => Array
(
)

[custom_result_object] => Array
(
)

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

http://blog.csdn.net/cindylu520/article/details/4098360

$rows = mysql_fetch_assoc($query);

可以使用mysql_fetch_array(data,array_type)函數
參數:
data —— 查詢的結果集
array_type ——MYSQL_ASSOC(產生關聯陣列),MYSQL_NUM(產生數字數組),MYSQL_BOTH(預設,同時產生關聯和數字數組)

取出數組,for迴圈組裝成新數組形式就好了

CI架構用 var_dump($result->result_array()); 列印出關聯陣列

手冊說明:
result_array()
該方法執行成功時將記錄集作為關聯陣列返回。失敗時返回空數組。一般情況下,我們使用下面的方法遍曆結果,代碼就像這樣:

$query = $this->db->query("要執行的 SQL");

foreach ($query->result_array() as $row)
{
echo $row['title'];
echo $row['name'];
echo $row['body'];
}

$result = array();while($row = mysql_fetch_assoc($query)){    $result[] = $row;}print_r($result);
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.