HTML表没有从mysql db填充

时间:2018-01-17 03:55:04

标签: php html mysql

我试图在我的mysql数据库的表中获得一个简单的数据HTML表,但它只是在页面上的一个段落中显示为混乱的文本和代码。可能导致这种情况的任何想法?以下是我正在运行的代码:

<html lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>
        Title
    </title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- Bootstrap -->
    <link href="./swipe_da_code_files/bootstrap.min.css" rel="stylesheet">
    <link rel="icon" type="image/ico" href="http://lndapp.wpi.edu/img/favicon.ico">

    <style type="text/css">
        body {
            padding-top: 60px;
            padding-bottom: 40px;
        }
    </style>
</head>
<body>
<nav class="navbar navbar-inverse navbar-fixed-top">
    <div class="container">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="http://url:Port">AdminPortal</a>
        </div>
        <div id="navbar" class="collapse navbar-collapse">
            <ul class="nav navbar-nav">
                <li class="active"><a href="default.htm">Home</a></li>
                <li><a href="CreateUser.html">Create User</a></li>
                <li><a href="Database.html">Database</a></li>
            </ul>
        </div><!--/.nav-collapse -->
    </div>
</nav>

<div class="container">
    <h1>Database Landing Page</h1>
</div>


<?php
  $server = mysql_connect("server", "user", "password"); 
  $db = mysql_select_db("database", $server); 
  $query = mysql_query("SELECT * FROM Table"); 
?>
         <table>
            <tr>
                <td>username</td>
                <td>password</td>
                <td>physicianID</td>
            </tr>

            <?php
               while ($row = mysql_fetch_array($query)) {
                   echo "<tr>";
                   echo "<td>".$row[username]."</td>";
                   echo "<td>".$row[password]."</td>";
                   echo "<td>".$row[physicianID]."</td>";
                   echo "</tr>";
               }
            ?>
        </table>

</body></html>

我尝试了各种不同的方式来显示数据,这是我能想到的最简单的表格,只是以表格形式显示几行,但它没有运行。我仔细检查了我输入的连接详细信息,以及数据库的查询,并确定不是导致它的原因。

This is an image of the website output based on the code

2 个答案:

答案 0 :(得分:2)

替换

           echo "<td>".$row[username]."</td>";
           echo "<td>".$row[password]."</td>";
           echo "<td>".$row[physicianID]."</td>";

通过

           echo "<td>".$row['username']."</td>";
           echo "<td>".$row['password']."</td>";
           echo "<td>".$row['physicianID']."</td>";

你缺少报价!

答案 1 :(得分:1)

请使用mysqli或PDO,因为现在不推荐使用mysl方法。

<?php
$server=mysqli_connect("server", "user", "password" , "my_db");

if (mysqli_connect_errno())
{
   echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result = mysqli_query($server, "SELECT * FROM Table")
?>

然后

<?php
   while ($row = mysqli_fetch_array($result))
   {
       echo "<tr>";
       echo "<td>".$row['username']."</td>";
       echo "<td>".$row['password']."</td>";
       echo "<td>".$row['physicianID']."</td>";
       echo "</tr>";
    }
 ?>

Akintunde007在他/她的答案中是正确的,你正在解析一个常量。