php 将数据库中的数据显示到html表中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20802539/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
display data from database into html table
提问by lelouchliana
I'm trying to display data from database into a table in html. here is my code:
我正在尝试将数据库中的数据显示到 html 表格中。这是我的代码:
php code:
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>";}
}
?>
html code:
html代码:
<form id="list_of_user" method="post" action="user_list.php" accept-charset='UTF-8'>
<h2> Table Example</h2>
<p> </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>
I have php and html in one page.
我在一页中有 php 和 html。
originally, I have a html table ready to display the data, but it won't show up. so I changed it into php. but the page goes here and there. . i'm using template for the page.
最初,我有一个 html 表准备显示数据,但它不会显示。所以我把它改成了php。但页面到处都是。. 我正在为页面使用模板。
Can you please show me how to ..say. pass the data from php to html??
你能告诉我如何..说。将数据从php传递到html?
回答by Moduo
Here is the solution total html with php and database connections
这是带有php和数据库连接的解决方案总html
<!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>
Source: retrieve data from db and display it in table in php .. see this code whats wrong with it?
回答by MillaresRoo
You must take echo "</table>";out of the while loop.
您必须echo "</table>";退出 while 循环。

