【发布时间】: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循环一样显示出来。
【问题讨论】:
-
不要在循环内返回。