将 SQL 数据库中的数据显示到 php/html 表中 [英] display data from SQL database into php/ html table

查看:39
本文介绍了将 SQL 数据库中的数据显示到 php/html 表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 phpmyadmin (MySQL) 上有一个数据库,我想在 HTML 或 PHP 表上显示我的 SQL 表之一.我在网上搜索过,无法实现此功能.有人可以帮我编码吗?

I have a database on phpmyadmin (MySQL) and I want to display one of my SQL tables on a HTML or PHP table. I have searched online and cannot implement this feature. Could someone please help me with the coding?

database = 'hrmwaitrose'
username = 'root'
host = 'localhost'

没有密码.

我想显示来自员工"的数据表.

I would like to display the data from the "employee" table.

推荐答案

你说你在 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 循环中(每次遇到结果行时都会运行),我们 echo 创建一个新的表行.我还添加了一个以包含字段.

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.

请注意mysql_fetch_array"自 PHP 5.5.0 起已弃用,并在 PHP 7.0.0 中删除.所以请看一下mysqli_fetch_array()";

这篇关于将 SQL 数据库中的数据显示到 php/html 表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆