从MySQL数据库中获取整数值

时间:2012-05-05 17:21:21

标签: php mysql

基本上我有一个MySQL数据库和一个名为users的表,其中有一个名为credits的列。如何获取当前登录用户的整数值?

我的专栏是:

  • USER_ID
  • 用户名
  • 电子邮件
  • 密码
  • 作用

3 个答案:

答案 0 :(得分:1)

我假设您首先要对您的用户进行身份验证。您可以使用任意数量的可用库或滚动自己的库。以下是一些开始的地方:

获得经过身份验证的用户的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

复制/粘贴的

答案 1 :(得分:0)

如果您知道当前登录用户的user_id,用户名或电子邮件,您的SQL查询将是:

"SELECT `credits` FROM `users` WHERE `user_id` = '$user_id'"
"SELECT `credits` FROM `users` WHERE `username` = '$username'"
"SELECT `credits` FROM `users` WHERE `email` = '$email'"

我假设您的用户表名为"users"

答案 2 :(得分:0)

对此不太确定,因为我通常使用PDO,但我想它一开始应该没问题

<?php
$db = mysql_connect("localhost", "user", "password");

$sql = "SELECT credits FROM users WHERE user_id = " . mysql_real_escape_string($_SESSION['userid']);

$result = mysql_query($sql);

$row = mysql_fetch_object($result);

$credits = $row->credits;

echo $credits;