2016-11-23 79 views
-1

嘗試在Laravel中使用mysql_fetch_array()時出現錯誤。Laravel無法使用mysql_fetch_array()

while ($row = mysql_fetch_array($query)) 

什麼使用,而不是mysql_fetch_array()?

錯誤我,

mysql_fetch_array()預計參數1是資源,數組給定

完整部分,

//prepare the tag cloud array for display 
    $terms = array(); // create empty array 
    $maximum = 0; // $maximum is the highest counter for a search term 

    $query = DB::select('SELECT term, counter FROM search ORDER BY counter DESC LIMIT 30'); 

    while ($row = mysql_fetch_array($query)) 
    { 
     $term = $row['term']; 
     $counter = $row['counter']; 

     // update $maximum if this term is more popular than the previous terms 
     if ($counter > $maximum) $maximum = $counter; 

     $terms[] = array('term' => $term, 'counter' => $counter); 

    } 
+0

添加完整的東西 –

+1

爲什麼?你爲什麼要這樣做???只需使用'DB'功能 – aynber

+0

如何在這裏使用DB功能? D B::?? – Nim

回答

4

DB語句和選擇返回結果,不是一個查詢對象,所以你只需要遍歷結果。

$results = DB::select('SELECT term, counter FROM search ORDER BY counter DESC LIMIT 30'); 

foreach($results as $row) 
{ 
    $term = $row->term; 
    $counter = $row->counter; 

    // update $maximum if this term is more popular than the previous terms 
    if ($counter > $maximum) $maximum = $counter; 

    $terms[] = array('term' => $term, 'counter' => $counter); 

} 

有關通過數據庫類運行原始查詢的詳細信息,請參閱here

+0

現在它生成一個錯誤,因爲AdminViewController.php中的FatalErrorException第84行: 不能使用類型爲stdClass的對象作爲數組。第84行是「$ term = $ row ['term'];」 – Nim

+0

啊,我永遠不會記得它是一個對象還是數組,我認爲它是在4到5之間改變的。我會用它作爲對象更新我的答案。 – aynber

+0

現在它工作!我是新來的拉拉維爾。這就是爲什麼我沒有注意到他們。謝謝! :) – Nim