Fetch all results from database using mysqli

You can just return the array from function and then loop in your script

while($row = $result->fetch_assoc())
{
    $rows[] = $row;
}
return $rows;

The you can loop in your script

$test = $connectTest->grabResults(test, id, id);
foreach($test as $value)
{
     print_r($value);
}

Upon OP edit

If you need to print them separate you can access all elements with variable name and scopes with keys as follow

$test = $connectTest->grabResults(test, id, id);
echo '<table>';
foreach($test as $value)
{
     echo '<tr>
             <td>'.$value['id'].'</td>
             <td>'.$value['name'].'</td>
             <td>'.$value['status'].'</td>
           </tr>';
}           
echo '</table>';

It looks like you are returning a single row of your results with this bit of the function:

        foreach ($rows as $resultData)
        {
            return $resultData;
        }

You should just return the whole thing instead.

    while($row = $result->fetch_assoc())
    {
        $rows[] = $row;
    }
    return $rows;

return inside foreach() iteration means stop right after first iteration. Therefore you will be always getting only the first result.

You'd better write this as:

   public function grabResults($table, $field, $id)
   {
        $result = $this->db->mysqli->query("SELECT * FROM $table WHERE $field = $id");

        $rows = array();

        while($row = $result->fetch_assoc()) {
            $rows[] = $row;
        }

        return $rows;
    }

Tags:

Php

Oop

Fetch