[英]How can I list result from sql to html table
So I have simple table in database: "users" 所以我在数据库中有简单的表:“用户”
------------------------------------------------------
| id | fname | sname | uname | pw |
------------------------------------------------------
| 1 | John | Doe | jdoe | 123 |
------------------------------------------------------
| 2 | John1 | Doe1 | jdoe1 | 234 |
------------------------------------------------------
| 3 | John2 | Doe2 | jdoe2 | 345 |
------------------------------------------------------
etc. I want to make html table where i can edit these values in database. 我想制作html表,我可以在数据库中编辑这些值。 How can print out all users user first and last names from that table? 如何从该表打印出所有用户的用户名和姓?
$sql= mysqli_query("SELECT * FROM users");
$result= mysqli_fetch_array($sql);
$count=count($result);
for ($x=1;$x<$count;$x++)
{
echo $result['fname']." ".$result['sname'];
}
I have this kind of code to make the user list but that doesn't work. 我有这种代码来制作用户列表,但这不起作用。
Try to loop while fetching data from database with while
尝试使用while
从数据库中获取数据时进行循环
$sql= mysqli_query("SELECT * FROM users");
while($result= mysqli_fetch_array($sql))
{
echo $result['fname']." ".$result['sname'] . '<br>';
}
Try this code: 试试这段代码:
echo "<table><tr><td>id</td><td>fname</td><td>sname</td><td>uname</td><td>pw</td></tr>";
$sql = mysqli_query("SELECT * FROM users");
while($result = mysqli_fetch_array($sql))
{
echo "<table><tr><td>".$result['id']."</td><td>".$result['fname']."</td><td>".$result['sname']."</td><td>".$result['uname']."</td><td>".$result['pw']."</td></tr>";
}
echo "</table>";
Anyways this is a rough example. 无论如何,这是一个粗略的例子。 Try never yo print HTML with a echo or print, instead of that close the PHP tag, print the HTML and open it again to avoid unnecessary server work. 尝试永远不要用回显或打印来打印HTML,而不是关闭PHP标记,打印HTML并再次打开它以避免不必要的服务器工作。
Also try to use PDO to connect to the database: PHP PDO Manual 还尝试使用PDO连接到数据库: PHP PDO手册
Not the ideal way but you could always change your SQL statement to this: 不是理想的方法,但您可以随时将SQL语句更改为:
SELECT '<table>' AS x
UNION ALL
SELECT
'<tr><td>id</td><td>fname</td><td>' +
'sname</td><td>uname</td><td>pw</td></tr>'
UNION ALL
SELECT
'<tr><td>' + id + '</td><td>' +
fname + '</td><td>' +
sname + '</td><td>' +
uname + '</td><td>' +
pw + '</td></tr>'
FROM USERS
UNION ALL
SELECT '</table>'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.