由於一些用戶已經指出瞭解決辦法,我只解釋爲什麼你沒有得到這個錯誤,這樣你就可以更好地理解查詢結果笨給人。
此錯誤:
But I got an error when I tried to echo it : Object of class CI_DB_mysql_result could not be converted to string
是因爲你試圖呼應的對象。
這段代碼
$terdaftar = $this->db->get_where('msumat', array('nama' => $nama));
會返回一個對象,這個對象將有關於你所做的查詢信息。 有了這個對象,你可以得到的結果(行)爲對象這樣做:
$results = $terdaftar->result();
或者你如果你感覺更舒服陣列可以返回的結果(行)作爲數組這樣做:
$results = $terdaftar->result_array();
您還可以得到的結果數這樣做:
$number_results = $terdaftar->num_rows()
而這僅僅是一個例子,你可以閱讀更多關於這裏的結果 http://ellislab.com/codeigniter/user-guide/database/results.html
編輯 一個更好的解釋:假設我們使用result_array()函數來得到結果在一個純粹的陣列格式:
$results = $terdaftar->result_array();
現在你的變量$結果是一個數組,迭代通過它,並得到你想要的,你會做這樣的事情的數據:
foreach ($results as $key => $row) {
//the row variable will have each row of your database returned by your query
//so if you want to access a field from that row,
//let's say for example the name field. You would do something like this
if($row['name']=='John')
echo $row['name'];
}
非常感謝您的幫助:D – 2013-03-07 12:04:07
我已更新我的答案,以便您更好地瞭解如何從一行中獲得特定字段 – 2013-03-07 12:10:34