Nim Nim - 15 days ago 10
MySQL Question

Laravel cannot use mysql_fetch_array()

I get a error when trying to use mysql_fetch_array() within Laravel.

while ($row = mysql_fetch_array($query))


What to use instead mysql_fetch_array()?

Error I got,


mysql_fetch_array() expects parameter 1 to be resource, array given


full part,

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

}

Answer

DB statements and selects return the results, not a query object, so you just need to iterate through the results.

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

}

More information on running raw queries through the DB class can be found here.