简体   繁体   English

如何从数据库中提取用户变量并将其显示在html / php中

[英]How to pull a User Variable from the database and display it in html/php

In my database I've created a table called accounts. 在数据库中,我创建了一个名为accounts的表。 The accounts table contains: id, username, password, points. 帐户表包含:ID,用户名,密码,积分。

This is the code I'm running to pull and display the Points variable. 这是我要拉出并显示Points变量的代码。

$user = $_SESSION['sess_user'];
$sql = mysql_query("SELECT points FROM accounts WHERE username='".$user."'");
$points = $sql;

Then a bit further down I use this in html. 再往下一点,我在html中使用它。

<p>Welkom, <?=$_SESSION['sess_user'];?>! Dit is jouw Managerpaneel. Je hebt momenteel <?=$points?> Managerpunten.</p>

Now, it does show the session Username, but it doesn't display anything for points :( it just completely skips it. 现在,它确实显示了会话用户名,但没有显示任何要点:(它只是完全跳过了它。

Any ideas? 有任何想法吗?

You are not actually fetching your results. 您实际上并没有获取结果。 mysql_query() returns a resource ID. mysql_query()返回资源ID。 You need to pass this to a function like mysql_fetch_assoc() to get your actual results. 您需要将此传递给类似mysql_fetch_assoc()的函数以获取实际结果。

$sql = mysql_query("SELECT points FROM accounts WHERE username='".$user."'");
$row = mysql_fetch_assoc($sql);
$points = $row['points'];

Please, don't use mysql_* functions in new code . 请不要在新代码中使用mysql_*函数 They are no longer maintained and are officially deprecated . 它们不再维护,已正式弃用 See the red box ? 看到红色框了吗? Learn about prepared statements instead, and use PDO , or MySQLi - this article will help you decide which. 而是了解准备好的语句 ,并使用PDOMySQLi- 本文将帮助您确定哪一个。 If you choose PDO, here is a good tutorial . 如果您选择PDO, 这是一个很好的教程

you are missing this: 您缺少此:

$row = mysql_fetch_array($sql);
$points = $row['points'];

for more info: mysqlfetcharray 有关更多信息: mysqlfetcharray

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM