【问题标题】:cant show data in table [closed]无法在表格中显示数据[关闭]
【发布时间】:2021-05-23 06:08:47
【问题描述】:

我试图将数据库中的数据显示到 html 表中。但是当我运行它时,它不会显示数据库数据。它只是像这样显示,'.$res['name'].'。我认为我犯了一个愚蠢的错误,但无法发现。

编辑:对不起,这是一个愚蠢的错误。我已将文件保存在 .html 扩展名中,我将其保存在 .php 中,现在它工作正常。感谢大家。 我的 view.php 文件:

<?php
include_once ("config.php");

$result = mysqli_query($mysqli,"SELECT* FROM `testing` ORDER BY `id` DES");
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <table border="1">
        <thead>
            <tr>
                <th>Name</th>
                <th>Email</th>
                <th>Phone</th>
            </tr>
        </thead>
 <?php
while($res=mysqli_fetch_array($result)){
    echo '<tr>';
    echo '<td>'.$res['name'].'</td>'; // here it just shows up like '.$res['name'].'</td>' , instead of showing data.
    echo '<td>'.$res['email'].'</td>';
    echo '<td>'.$res['phone'].'</td>';
    echo '</tr>';
}
?>

</table>

 


    
</body>
</html>

我的 config.php 文件:

<?php

$dbhost = "localhost";
$dbname = "datatest";
$dbuser = "root";
$dbpwd = "";

$mysqli = mysqli_connect($dbhost,$dbuser,$dbpwd,$dbname);
?>

【问题讨论】:

  • 如果您在屏幕上看到这样的 PHP 代码,我想知道 PHP 是否没有安装,或者您的网络服务器中没有正确配置,或者您是否没有通过正确的方式访问页面HTTP
  • 一旦你修复了这个问题,你应该注意你的查询中的错误,如下面 user27976 的回答中提到的

标签: php html database


【解决方案1】:
$result = mysqli_query($mysqli,"SELECT * FROM `testing` ORDER BY `id` DESC");

select和*前加空格,“Des”不正确,“DESC”正确

【讨论】:

    【解决方案2】:

    您可能需要设置 mysqli_fetch_array $mode MYSQLI_ASSOC 来指定您想要一个关联数组。

    PHP

    while($res=mysqli_fetch_array($result, MYSQLI_ASSOC)){
    

    【讨论】:

    • 它默认为MYSQLI_BOTH,所以没有它也可以工作。
    【解决方案3】:

    fetch_array() / mysqli_fetch_array() 函数将结果行作为关联数组、数值数组或两者兼而有之。

    如果要将结果行作为关联数组提取,

    while($res=mysqli_fetch_array($result,MYSQLI_ASSOC))

    如果你想要一个数值数组的结果,

    while($res=mysqli_fetch_array($result, MYSQLI_NUM))

    否则,您需要两个 [MYSQLI_BOTH(这是默认设置)]

    while($res=mysqli_fetch_array($result, MYSQLI_BOTH))

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-30
      • 2015-12-11
      相关资源
      最近更新 更多