在 html 页面上显示来自 mysql/phpmyadmin 的数据


Display data from mysql/phpmyadmin on html page

我是PHP的初学者。我尝试将PHP放在HTML文件中,以便从我的数据库中调用名为test的数据和名为myemployee的表中调用数据。
我尝试了其他论坛的所有建议,但仍然没有在表格中显示,而是显示了代码。
按照我的代码。我的代码有问题吗?

即使我放入PHP文件,然后像这样从HTML文件调用<?php include ('test.php')?>它仍然不起作用。

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Home</title>
<link href="style/index-layout.css" rel="stylesheet" type="text/css" />
<link href="style/homepage-layout.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<!--right-->
<div id="body">
<div id="left">
<br /><br />
<P ><B><h3>Progress</h3></FONT></B></P>
</div></div>
<?php
      $username = "root";
      $password = "";
      $host = "localhost";
      $connector = mysql_connect($host,$username,$password)
          or die("Unable to connect");
        echo "Connections are made successfully::";
      $selected = mysql_select_db("test", $connector)
        or die("Unable to connect");
      //execute the SQL query and return records
      $result = mysql_query("SELECT * FROM myemployee");
      ?>
<table>
      <thead>
        <tr>
          <th>Employee_id</th>
          <th>Employee_Name</th>
          <th>Employee_dob</th>
          <th>Employee_Adress</th>
          <th>Employee_dept</th>
          <th>Employee_salary</th>
        </tr>
      </thead>
      <tbody>
        <?php
          while( $row = mysql_fetch_assoc( $result ) ){
            echo "$row";
            echo
            "<tr>
              <td>{$row['employee_id']}</td>
              <td>{$row['employee_name']}</td>
              <td>{$row['employee_dob']}</td>
              <td>{$row['employee_addr']}</td>
              <td>{$row['employee_dept']}</td>
              <td>{$row['employee_sal']}</td> 
            </tr>";
          }
        ?>
      </tbody>
    </table>
     <?php mysql_close($connector); ?>
<br/><br/>
</body>
</html>

无需将其调用到 html 文件。您可以将整个代码放在一个.php文件中并运行 php 文件。我只是检查了 E3d 您的代码,并将其放入 PHP 文件中并进行了一些更改。它工作得很好(显然没有风格)。这是代码:

<?php
 $username = "root";
 $password = "";
 $host = "localhost";
 $connector = mysql_connect($host, $username, $password)
    or die("Unable to connect");
 $selected = mysql_select_db("sample", $connector)
    or die("Unable to connect");
 ?>
 <!DOCTYPE html>
 <html xmlns="http://www.w3.org/1999/xhtml">
 <head>
    <title>Home</title>
    <link href="style/index-layout.css" rel="stylesheet" type="text/css" />
    <link href="style/homepage-layout.css" rel="stylesheet" type="text/css"/>
 </head>
 <body>
    <!--right-->
    <div id="body">
        <div id="left">
        </div></div>
    <?php
    //execute the SQL query and return records
    $result = mysql_query("SELECT * FROM alte_brand");
    ?>
    <table>
        <thead>
            <tr>
                <th>Employee_id</th>
                <th>Employee_Name</th>
            </tr>
        </thead>
        <tbody>
            <?php
            while ($row = mysql_fetch_assoc($result)) {
                echo
                "<tr>
          <td>{$row['bid']}</td>
          <td>{$row['bname']}</td>
        </tr>";
            }
            ?>
        </tbody>
    </table>
 </body>
 </html>
<?php mysql_close($connector); ?>

检查您的文件扩展名。PHP(默认情况下)只能在以.php为扩展名的文件中运行。

您可以将此指令添加到 .htaccess 文件中。

AddType application/x-httpd-php .html