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 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的