2012-10-19 71 views
1
function random() 
    { 
    $anketadb = $this->load->database('anketa',TRUE); 
    $br = $anketadb->count_all_results('anketadata'); 
    $nmb = mt_rand(1,$br); 

    if ($nmb != 1){ 
    $nmb = $nmb - 1; 
    } 

    $count = $anketadb->get('anketadata', 1, $nmb); 

    return $count; 
    } 

Why this code when i echo it in View returns ERROR:笨隨機行的MySQL無錯誤對象無法轉換爲字符串

A PHP Error was encountered

Severity: 4096

Message: Object of class CI_DB_mysqli_result could not be converted to string

Filename: ankete/rezultatiankete.php

Line Number: 52

回答

1

您應該向我們展示了更多代碼(控制器,視圖等),無論如何,在您正在使用的示例中

return $count; 
在這種情況下 $count;

是一個對象,呼應這是你必須循環領域在你看來像

foreach ($count->result() as $row) 
{ 
    echo $row->fieldname; // rerplace the fieldname with a real field/column name of your database 
} 

所以,如果你想呼應$count,那麼你犯了一個錯誤,這是一個對象,read more here

+0

謝謝,而不是如何迴應它,以某種方式提示foreach? – Daniela

+0

是的,在foreach裏只是回顯列名像'回聲$ row-> fieldname;' –

+0

但是這是模型,所以在視圖中,你可以告訴我該怎麼做? – Daniela

0

echo多年平均值的工作,陣列

抓取隨機列中使用SELECT * FROM table ORDER BY RAND() LIMIT 0,1;

+0

如果我使用的print_r其打印是這樣的: 「CI_DB_mysqli_result對象([conn_id] => mysqli的對象([affected_rows] => 1 [CLIENT_INFO] => 52年5月1日[client_version] => 50152 [connect_errno] => 0 [server_info] => 0 [connect_error] => [errno] => 0 [error] => [field_count] => 15 [host_info] => Localhost通過UNIX套接字[info] => [insert_id] 5.1.61-log [server_version] => 50161 [sqlstate] => 00000 [protocol_version] => 10 [thread_id] => 18494924 [warning_count] => 0)[resul .....' – Daniela

+0

你想要什麼細節數據庫 –

+0

一個隨機的行數據。 – Daniela