Laravel 不能使用 mysql_fetch_array()

Nim*_*Nim -1 php mysql query-builder laravel

尝试在 Laravel 中使用 mysql_fetch_array() 时出现错误。

while ($row = mysql_fetch_array($query))
Run Code Online (Sandbox Code Playgroud)

用什么来代替 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);

    }
Run Code Online (Sandbox Code Playgroud)

ayn*_*ber 6

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);

}
Run Code Online (Sandbox Code Playgroud)

可以在此处找到有关通过 DB 类运行原始查询的更多信息。