douguan3470 2014-03-20 08:37
浏览 328
已采纳

使用mysqli从数据库中获取所有结果

please check out my code below. With that class I am able to display results like so:

$connectTest    = new testResults();
$test       = $connectTest->grabResults(test, id, id);

echo $test['id'];
echo $test['name'];
echo $test['address'];

In my database I have several fields in the "test" table. I go to my page using index.php?id=1. With this I am displaying just the results from one row because it grabs all results WHERE id = 1.

What I need is the class below to display multiple results. It just displays one row. But if I have multiple rows with id = 1 I would like to display these results, but I cannot get it to work. I have tried a lot of things but I always end up with just one result.

class:

class testResults
{

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

        $resultData[] = array();

        if(!$result)
        {
            return false;
        }

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

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

Edit:

Array ( [id] => 25 [name] => test [status] => 1 )
Array ( [id] => 25 [name] => test [status] => 3 )
Array ( [id] => 25 [name] => test [status] => 5 )
Array ( [id] => 25 [name] => test [status] => 4 )

Array ( [id] => 26 [name] => test [status] => 1 )
Array ( [id] => 26 [name] => test [status] => 3 )

Array ( [id] => 27 [name] => test [status] => 1 )
Array ( [id] => 27 [name] => test [status] => 3 )
Array ( [id] => 27 [name] => test [status] => 5 )
Array ( [id] => 27 [name] => test [status] => 4 )
Array ( [id] => 27 [name] => test [status] => 2 )
Array ( [id] => 27 [name] => test [status] => 4 )
Array ( [id] => 27 [name] => test [status] => 1 )

I am getting results as above, any way to easily display these results in an echo? For each id there are different results, so results will vary with each query. So I would like to display results in a table for example like so:

echo '<table>
<tr>
<td>$id</td>
<td>$name</td>
<td>$status</td>
</tr>
</table>';

So all results will be displayed like in a while loop.

  • 写回答

4条回答 默认 最新

  • duanchu0031 2014-03-20 08:39
    关注

    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>';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?