沃梦达 / 编程问答 / php问题 / 正文

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

Fetch all results from database using mysqli(使用 mysqli 从数据库中获取所有结果)

本文介绍了使用 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'];

在我的数据库中,测试"表中有几个字段.我使用 index.php?id=1 访问我的页面.有了这个,我只显示一行的结果,因为它会抓取所有结果 WHERE id = 1.

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.

我需要的是下面的类来显示多个结果.它只显示一行.但是,如果我有多个 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 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;
            }
    }
}

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 )

我得到了上述结果,有什么方法可以轻松地在回显中显示这些结果?对于每个 id 都有不同的结果,因此结果会因每个查询而异.所以我想在表格中显示结果,例如:

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

所以所有的结果都会像while循环一样显示出来.

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

推荐答案

你可以只从函数中返回数组,然后在你的脚本中循环

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

在 OP 编辑​​时

如果您需要单独打印它们,您可以使用以下键访问具有变量名称和范围的所有元素

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

这篇关于使用 mysqli 从数据库中获取所有结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:使用 mysqli 从数据库中获取所有结果

基础教程推荐