将数据从SQL数据库显示到php / html表格中 [英] display data from SQL database into php/ html table
问题描述
OK我有一个关于phpmyadmin(sql)的数据库,我想将我的一个表格显示在HTML / PHP表格中。我在网上搜索,无法实现这个功能,所以我想知道是否有人可以帮我编码在这个?
OK I have a database on phpmyadmin (sql) and I want to display one of my tables into a table on HTML/PHP. I have searched online and can not implement this feature, so I'm wondering if someone could help me with the coding on this?
database = 'hrmwaitrose'
username = 'root'
host = 'localhost'
NO PW
我想要显示来自表名员工
推荐答案
你说你有一个关于PhpMyAdmin的数据库,所以你使用的是MySQL。 PHP提供了连接到MySQL数据库的函数。
You say you have a database on PhpMyAdmin, so you are using MySQL. PHP provides functions for connecting to a MySQL database.
$connection = mysql_connect('localhost', 'root', ''); //The Blank string is the password
mysql_select_db('hrmwaitrose');
$query = "SELECT * FROM employee"; //You don't need a ; like you do in SQL
$result = mysql_query($query);
echo "<table>"; // start a table tag in the HTML
while($row = mysql_fetch_array($result)){ //Creates a loop to loop through results
echo "<tr><td>" . $row['name'] . "</td><td>" . $row['age'] . "</td></tr>"; //$row['index'] the index here is a field name
}
echo "</table>"; //Close the table in HTML
mysql_close(); //Make sure to close out the database connection
在while循环中(每当我们遇到时结果行),我们回显它创建一个新的表格行。我还添加了一个包含字段。
In the while loop (which runs every time we encounter a result row), we echo which creates a new table row. I also add a to contain the fields.
这是一个非常基本的模板。您可以使用mysqli_connect而不是mysql_connect查看其他答案。 mysqli代表mysql的改进。它提供了更多的功能。你注意到它也有点复杂。这取决于你需要什么。
This is a very basic template. You see the other answers using mysqli_connect instead of mysql_connect. mysqli stands for mysql improved. It offers a better range of features. You notice it is also a little bit more complex. It depends on what you need.
这篇关于将数据从SQL数据库显示到php / html表格中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!