将数据从数据库显示到html表中

lel*_*ana 0 html php mysql

我正在尝试将数据库中的数据显示为html中的表格.这是我的代码:

php代码:

if($_SERVER['REQUEST_METHOD'] =='POST')
{   
 $type_user=$_POST['type_user'];
 $sql="SELECT staff_id, name, email, role FROM user WHERE role='$type_user'";

 $run= $db->query($sql)
    or die($db -> error);

 $num=mysqli_num_rows($run);
 $row=mysqli_fetch_array($run, MYSQLI_ASSOC);
 //$yana =  $row['staff_id'];
 //echo "dd".$yana;

 echo "<table >
  <tr>
    <td >Staff ID </td>
    <td >Name</td>
    <td >Email</td>
    <td >Role</td>
  </tr>";

  while($row = mysqli_fetch_array($run, MYSQLI_ASSOC))
  {
  echo "<tr>";
  echo "<td>".$row['staff_id']."</td>";
  echo "<td>".$row['name']."</td>";
  echo "<td>".$row['email']."</td>";
  echo "<td>".$row['role']."</td>";
  echo "</tr>";
  echo "</table>";}

 }
 ?>
Run Code Online (Sandbox Code Playgroud)

HTML代码:

<form id="list_of_user"  method="post" action="user_list.php" accept-charset='UTF-8'>

<h2> Table Example</h2>
<p>&nbsp;</p>
<table width="729" border="0" >
  <tr valign ="center">
    <td width="85" valign ="center">User: </td>
    <td width="196" valign ="center"><select name="type_user">
      <option value="TELLER" selected="selected">TELLER</option>
      <option value="MANAGER">MANAGER</option>
    </select>          </td>
    <td width="97" valign ="center"><input name="Go" type="submit" id="Go" value="Go"     /></td>
  </tr>
  </table>
Run Code Online (Sandbox Code Playgroud)

我在一个页面中有php和html.

最初,我有一个html表准备显示数据,但它不会显示.所以我把它改成了php.但页面到处都是..我正在使用页面模板.

你能告诉我怎么样的.say.将数据从php传递给html ??

Mod*_*duo 7

这是解决方案总PHP与PHP和数据库连接

   <!doctype html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <title>database connections</title>
    </head>
    <body>
      <?php
      $username = "database-username";
      $password = "database-password";
      $host = "localhost";

      $connector = mysql_connect($host,$username,$password)
          or die("Unable to connect");
        echo "Connections are made successfully::";
      $selected = mysql_select_db("test_db", $connector)
        or die("Unable to connect");

      //execute the SQL query and return records
      $result = mysql_query("SELECT * FROM table_one ");
      ?>
      <table border="2" style= "background-color: #84ed86; color: #761a9b; margin: 0 auto;" >
      <thead>
        <tr>
          <th>Employee_id</th>
          <th>Employee_Name</th>
          <th>Employee_dob</th>
          <th>Employee_Adress</th>
          <th>Employee_dept</th>
          <td>Employee_salary</td>
        </tr>
      </thead>
      <tbody>
        <?php
          while( $row = mysql_fetch_assoc( $result ) ){
            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>\n";
          }
        ?>
      </tbody>
    </table>
     <?php mysql_close($connector); ?>
    </body>
    </html>
Run Code Online (Sandbox Code Playgroud)

来源:从数据库中检索数据并在php中将其显示在表中..看到这段代码有什么不对吗?