我正在尝试将数据库中的数据显示为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 "
Staff ID | Name | Role | |
".$row['staff_id']." | "; echo "".$row['name']." | "; echo "".$row['email']." | "; echo "".$row['role']." | "; echo "
HTML代码:
这是解决方案总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" 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>
来源:从数据库中检索数据并在php中将其显示在表中..看到这段代码有什么不对吗?