php 遍历数据库并显示在表中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/848905/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 00:04:30  来源:igfitidea点击:

loop through database and show in table

phpmysql

提问by Elliott

I am trying to loop though my users database to show each username in the table in their own row. I can only get it to show one user but loops through this the number of rows there are. Code is below

我试图通过我的用户数据库循环显示表中的每个用户名在他们自己的行中。我只能让它显示一个用户,但会循环显示有多少行。代码如下

<?php
require_once ('../login/connection.php');
include ('functions.php');

$query = "SELECT * FROM users";
$results=mysql_query($query);
$row_count=mysql_num_rows($results);
$row_users = mysql_fetch_array($results);

echo "<table>";
    for ($i=0; $i<$row_count; $i++)
    {
    echo "<table><tr><td>".($row_users['email'])."</td></tr>";
    }
    echo "</table>";
?>

Thanks

谢谢

回答by Paul Dixon

mysql_fetch_arrayfetches a single row - you typically use it in a while loop to eat all the rows in the result set, e.g.

mysql_fetch_array获取单行 - 您通常在 while 循环中使用它来处理结果集中的所有行,例如

echo "<table>";

while ($row_users = mysql_fetch_array($results)) {
    //output a row here
    echo "<tr><td>".($row_users['email'])."</td></tr>";
}

echo "</table>";

回答by Parrots

You're only fetching one row:

您只获取一行:

$row_users = mysql_fetch_array($results);

You're never calling a fetch again so you're not really looping through anything.

您永远不会再次调用 fetch,因此您并没有真正遍历任何内容。

I'd suggest changing your loop to the following:

我建议将您的循环更改为以下内容:

echo "<table>";
while ($row = mysql_fetch_array($results)) {
    echo "<tr><td>".($row['email'])."</td></tr>";
}
echo "</table>";

The while loop will loop through the results and assign a row to $row, until you run out of rows. Plus no need to deal with getting the count of results at that point. This is the "usual" way to loop through results from a DB in php.

while 循环将遍历结果并将一行分配给 $row,直到用完行。另外,此时无需处理获取结果的计数。这是在 php 中循环访问来自 DB 的结果的“通常”方式。

回答by JerryB

In the new MYSQLI, I use this coding

在新的 MYSQLI 中,我使用这种编码

$query = "SELECT * FROM users";
$results=mysqli_query($con,$query);
$row_count=mysqli_num_rows($results);

echo "<table>";
while ($row = mysqli_fetch_array($results)) {
echo "<tr><td>".($row['id'])."</td></tr>";
}
echo "</table>";

mysqli_query($con,$query); 
mysqli_close($con);

回答by Marc W

Your problem is in this line:

你的问题在这一行:

echo "<table><tr><td>".($row_users['email'])."</td></tr>";

You're echoing out a <table>tag again. Remove that so your code looks like this:

<table>再次呼应了一个标签。删除它,使您的代码如下所示:

echo "<table>";
    for ($i=0; $i<$row_count; $i++)
    {
    echo "<tr><td>".($row_users['email'])."</td></tr>";
    }
echo "</table>";

回答by Nick Presta

You don't need to output a table within a table the way you're doing it.

您不需要按照您的方式在表格中输出表格。

$result = mysql_query("SELECT `email` FROM `users`");
$num_emails = mysql_num_rows($result);

echo "<table><caption>There are/is $num_emails email(s)</caption>";
while ($row = mysql_fetch_assoc($result)) {
    echo "<tr><td>{$row['email']}</td></tr>";
}
echo '</table>';

Something like that should work.

这样的事情应该有效。