php 使用mysqli从数据库中获取所有结果
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22527465/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Fetch all results from database using mysqli
提问by Hardist
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.
在我的数据库中,“测试”表中有几个字段。我使用 index.php?id=1 进入我的页面。有了这个,我只显示一行的结果,因为它获取所有结果 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.
我需要的是下面的类来显示多个结果。它只显示一行。但是如果我有 id = 1 的多行,我想显示这些结果,但我无法让它工作。我尝试了很多东西,但我总是只得到一个结果。
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:
我得到了上述结果,有什么方法可以轻松地在回声中显示这些结果?对于每个 id 有不同的结果,因此每个查询的结果都会有所不同。所以我想在表格中显示结果,例如:
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.
因此,所有结果都将像在 while 循环中一样显示。
采纳答案by Fabio
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
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>';
回答by Fluffeh
It looks like you are returning a single row of your results with this bit of the function:
看起来您正在使用此函数返回单行结果:
foreach ($rows as $resultData)
{
return $resultData;
}
You should just return the whole thing instead.
你应该只返回整个东西。
while($row = $result->fetch_assoc())
{
$rows[] = $row;
}
return $rows;
回答by mickmackusa
Since you are passing the full resultset to another layer for processing, you can skip the loop to generate an array of associative arrays from the resultset.
由于您将完整的结果集传递给另一层进行处理,因此您可以跳过循环以从结果集生成关联数组的数组。
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");
if (!$result) {
return false;
}
return $result->fetch_all(MYSQLI_ASSOC); // just in case you wanted to see the column names
}
}
Then when you want to generate an html table from the returned array of associative arrays, use implode()
as a flexible solution that doesn't care if you ever change the number of columns being passed in -- it will handle an indefinite number of columns.
然后,当您想从返回的关联数组数组生成 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>";
}
回答by Yang
return
inside foreach()
iteration means stopright after first iteration. Therefore you will be always getting only the first result.
return
内部foreach()
迭代意味着在第一次迭代后立即停止。因此,您将始终只获得第一个结果。
You'd better write this as:
你最好把它写成:
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;
}