基本上我有一个 MySQL 数据库和一个名为 users
的表其中有一列名为 credits
。如何获取当前登录用户的整数值?
我的专栏是:
- 用户 ID
- 用户名
- 电子邮件
- 密码
- 角色
- 积分
最佳答案
我假设您首先要对您的用户进行身份验证。您可以为此使用任意数量的可用库,也可以推出您自己的库。这里有几个地方可以开始:
一旦您获得了经过身份验证的用户的 user_id,您就可以构建一个非常简单的 MySQL 查询来提取积分:
// Connect to the database
$connection = new mysqli("localhost", "mysql_user", "mysql_password", "database");
if (mysqli_connect_errno())
{
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
if ($statement = $mysqli->prepare("SELECT credits FROM user WHERE user_id=?"))
{
// $user_id is the stored value
$statement->bind_param("i", $user_id);
$statement->execute();
$statement->bind_result($credits);
$statement->fetch();
echo "User has " . $credits . "credits<br/>";
$statement->close();
}
/* close connection */
$connection ->close();
代码大部分是从 http://www.php.net/manual/en/mysqli.prepare.php 复制/粘贴的
关于php - 从 MySQL 数据库获取整数值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10464211/