如何从数据库(php)中回显表行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/2970936/
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
How to echo out table rows from the db (php)
提问by pg.
i want to echo out everything from a particular query. If echo $res I only get one of the strings. If I change the 2nd mysql_result argument I can get the 2nd, 2rd etc but what I want is all of them, echoed out one after the other. How can I turn a mysql result into something I can use?
我想从特定查询中回显出所有内容。如果 echo $res 我只得到其中一个字符串。如果我更改第二个 mysql_result 参数,我可以获得第二个、第二个等,但我想要的是所有这些,一个接一个地回显出来。如何将 mysql 结果变成我可以使用的结果?
I tried:
我试过:
$query="SELECT * FROM MY_TABLE";
$results = mysql_query($query);
$res = mysql_result($results, 0);
while ($res->fetchInto($row)) {
    echo "<form id=\"$row[0]\" name=\"$row[0]\" method=post action=\"\"><td style=\"border-bottom:1px solid black\">$row[0]</td><td style=\"border-bottom:1px solid black\"><input type=hidden name=\"remove\" value=\"$row[0]\"><input type=submit value=Remove></td><tr></form>\n";
}
回答by VoteyDisciple
$sql = "SELECT * FROM MY_TABLE";
$result = mysqli_query($conn, $sql); // First parameter is just return of "mysqli_connect()" function
echo "<br>";
echo "<table border='1'>";
while ($row = mysqli_fetch_assoc($result)) { // Important line !!! Check summary get row on array ..
    echo "<tr>";
    foreach ($row as $field => $value) { // I you want you can right this line like this: foreach($row as $value) {
        echo "<td>" . $value . "</td>"; // I just did not use "htmlspecialchars()" function. 
    }
    echo "</tr>";
}
echo "</table>";
回答by ToolmakerSteve
Expanding on the accepted answer:
扩展已接受的答案:
function mysql_query_or_die($query) {
    $result = mysql_query($query);
    if ($result)
        return $result;
    else {
        $err = mysql_error();
        die("<br>{$query}<br>*** {$err} ***<br>");
    }
}
...
$query = "SELECT * FROM my_table";
$result = mysql_query_or_die($query);
echo("<table>");
$first_row = true;
while ($row = mysql_fetch_assoc($result)) {
    if ($first_row) {
        $first_row = false;
        // Output header row from keys.
        echo '<tr>';
        foreach($row as $key => $field) {
            echo '<th>' . htmlspecialchars($key) . '</th>';
        }
        echo '</tr>';
    }
    echo '<tr>';
    foreach($row as $key => $field) {
        echo '<td>' . htmlspecialchars($field) . '</td>';
    }
    echo '</tr>';
}
echo("</table>");
Benefits:
好处:
- Using mysql_fetch_assoc (instead of mysql_fetch_array with no 2nd parameter to specify type), we avoid getting each field twice, once for a numeric index (0, 1, 2, ..), and a second time for the associative key. 
- Shows field names as a header row of table. 
- Shows how to get both - column name($key) and- value($field) for each field, as iterate over the fields of a row.
- Wrapped in - <table>so displays properly.
- (OPTIONAL) dies with display of query string and mysql_error, if query fails. 
- 使用 mysql_fetch_assoc(而不是没有第二个参数来指定类型的 mysql_fetch_array),我们避免获取每个字段两次,一次用于数字索引 (0, 1, 2, ..),第二次用于关联键。 
- 将字段名称显示为表的标题行。 
- 显示如何在遍历一行的字段时同时获取每个字段的 - column name($key) 和- value($field)。
- 包裹在 - <table>所以显示正确。
- (可选)如果查询失败,则会显示查询字符串和 mysql_error。 
Example Output:
示例输出:
Id      Name
777     Aardvark
50      Lion
9999    Zebra
回答by Babiker
 $result= mysql_query("SELECT * FROM MY_TABLE");
 while($row = mysql_fetch_array($result)){
      echo $row['whatEverColumnName'];
 }
回答by DeathPro
$sql = "SELECT * FROM YOUR_TABLE_NAME";
$result = mysqli_query($conn, $sql); // First parameter is just return of "mysqli_connect()" function
echo "<br>";
echo "<table border='1'>";
while ($row = mysqli_fetch_assoc($result)) { // Important line !!!
    echo "<tr>";
    foreach ($row as $field => $value) { // If you want you can right this line like this: foreach($row as $value) {
        echo "<td>" . $value . "</td>"; 
    }
    echo "</tr>";
}
echo "</table>";
In PHP 7.x You should use mysqli functions and most important one in while loop condition use "mysqli_fetch_assoc()" function not "mysqli_fetch_array()" one. If you would use "mysqli_fetch_array()", you will see your results are duplicated. Just try these two and see the difference.
在 PHP 7.x 中,您应该使用 mysqli 函数,最重要的一个在 while 循环条件中使用“mysqli_fetch_assoc()”函数而不是“mysqli_fetch_array()”函数。如果你使用“mysqli_fetch_array()”,你会看到你的结果是重复的。试试这两个,看看有什么不同。

