【问题标题】:Fetch all results from database using mysqli [duplicate]使用mysqli从数据库中获取所有结果[重复]
【发布时间】:2014-03-20 08:37:00
【问题描述】:

请在下面查看我的代码。通过该课程,我可以显示如下结果:

$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。

我需要的是下面的类来显示多个结果。它只显示一行。但是,如果我有多个 id = 1 的行,我想显示这些结果,但我无法让它工作。我尝试了很多东西,但我总是只得到一个结果。

类:

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 都有不同的结果,因此结果会因每个查询而异。所以我想在表格中显示结果,例如:

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

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

【问题讨论】:

  • 不要在循环内返回。

标签: php oop fetch


【解决方案1】:

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

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

你可以在你的脚本中循环

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

在 OP 编辑​​时

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

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

【讨论】:

    【解决方案2】:

    由于您将完整的结果集传递给另一层进行处理,因此您可以跳过循环以从结果集中生成关联数组的数组。

    class testResults {
        public function grabResults($table, $field, $id) {
            // For the record, I feel a prepared statement is in order here...
            $result = $this->db->mysqli->query("SELECT * FROM $table WHERE $field = $id");    
            return $result->fetch_all(MYSQLI_ASSOC);  // just in case you wanted to see the column names
        }
    }
    

    然后,当您想从返回的关联数组数组中生成一个 html 表时,请使用implode() 作为一种灵活的解决方案,它不关心您是否更改传入的列数——它会处理列数不定。

    if ($resultset = grabResults("tests", "name", "test")) {
        echo "<table>";
            foreach ($resultset as $i => $row) {
                // if (!$i) { echo "<tr><th>" , implode("</th><th>", array_keys($row)) , "</th></tr>"; }
                echo "<tr><td>" , implode("</td><td>", $row) , "</td><tr>"
            }
        echo "</table>";
    }
    

    【讨论】:

      【解决方案3】:

      看起来你正在使用这个函数返回一行结果:

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

      您应该只返回整个内容。

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

      【讨论】:

        【解决方案4】:

        return inside foreach() 迭代意味着在第一次迭代之后停止。因此,您将始终只获得第一个结果。

        你最好这样写:

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

        【讨论】:

          猜你喜欢
          • 2023-01-02
          • 1970-01-01
          • 2014-01-29
          • 1970-01-01
          • 2019-02-17
          • 2023-03-21
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多