Php 使用mysqli从数据库获取所有结果
请查看下面我的代码。使用该类,我可以显示如下结果:Php 使用mysqli从数据库获取所有结果,php,oop,fetch,Php,Oop,Fetch,请查看下面我的代码。使用该类,我可以显示如下结果: $connectTest = new testResults(); $test = $connectTest->grabResults(test, id, id); echo $test['id']; echo $test['name']; echo $test['address']; 在我的数据库中,“test”表中有几个字段。我使用index.php?id=1进入我的页面。这样,我只显示一行的结果,因为它获取id
$connectTest = new testResults();
$test = $connectTest->grabResults(test, id, id);
echo $test['id'];
echo $test['name'];
echo $test['address'];
在我的数据库中,“test”表中有几个字段。我使用index.php?id=1进入我的页面。这样,我只显示一行的结果,因为它获取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;
}
}
}
编辑:
我得到的结果如上所述,是否有任何方法可以轻松地在回音中显示这些结果?对于每个id都有不同的结果,因此每个查询的结果都会有所不同。因此,我想在表格中显示结果,例如:
echo '<table>
<tr>
<td>$id</td>
<td>$name</td>
<td>$status</td>
</tr>
</table>';
echo'
$id
$name
$status
';
因此,所有结果都将像在while循环中一样显示。看起来您正在使用此功能返回一行结果:
foreach ($rows as $resultData)
{
return $resultData;
}
你应该把整件东西都还回去
while($row = $result->fetch_assoc())
{
$rows[] = $row;
}
return $rows;
您可以从函数返回数组,然后在脚本中循环
while($row = $result->fetch_assoc())
{
$rows[] = $row;
}
return $rows;
$test = $connectTest->grabResults(test, id, id);
foreach($test as $value)
{
print_r($value);
}
您可以在脚本中循环
while($row = $result->fetch_assoc())
{
$rows[] = $row;
}
return $rows;
$test = $connectTest->grabResults(test, id, id);
foreach($test as $value)
{
print_r($value);
}
一经编辑
如果您需要单独打印它们,您可以使用变量名访问所有元素,并使用键访问范围,如下所示
$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>';
$test=$connectTest->grabResults(测试,id,id);
回声';
foreach($testas$value)
{
回声'
“.$value['id']”
“.$value['name']”
“.$value['status']”
';
}
回声';
返回内部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;
}
由于要将完整的结果集传递给另一层进行处理,因此可以跳过循环,从结果集生成关联数组数组
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表时,可以使用infrade()
作为一种灵活的解决方案,它不关心您是否更改传入的列数——它将处理无限数量的列
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>";
}
if($resultset=grabResults(“测试”、“名称”、“测试”)){
回声“;
foreach($resultset为$i=>$row){
//如果(!$i){echo',内爆(“,数组_键($row)),”;}
回显“”,内爆(“,$row)”
}
回声“;
}
不要在循环内返回。