【问题标题】:Ranking system using mysqli使用mysqli的排名系统
【发布时间】:2016-06-13 11:08:38
【问题描述】:

我想检查用户是否有一个用户的等级,它以 0、1、2 或 3 的整数形式存储在我的数据库中。

我希望它像 echo $my_rank; #不是说 0、1、2 或 3,而是说用户为 0,版主为 1,管理员为 2,所有者为 3。

这是我的代码:

$my_rank = $_SESSION['rank'] = array("owner","administrator","moderator","user");

$rank = array (
    '0' => 'user',
    '1' => 'moderator',
    '2' => 'administrator',
    '3' => 'owner'
);

config.php

<?php

# Error Reporting
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);

# No Error Reporting
//error_reporting(0);

# Message Responses
$success = array();
$danger = array();
$warning = array();
$info = array();

# Define the Database
define('DB_SERVER','localhost');
define('DB_USERNAME','blah');
define('DB_PASSWORD','blah');
define('DB_DATABASE','blah');

# Connect to the database
$con = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
if (!$con) {
    $danger[] = mysqli_connect_error();
} else {
    //echo "It worked";
}
?>

【问题讨论】:

标签: php arrays mysqli system ranking


【解决方案1】:

嗯...根据您提供给我的信息,似乎只需要一个查询即可从数据库中获取数据。然后我们可以回显它。

在您的数据库连接中的else 块中,我们可以执行以下操作:

# Connect to the database
$con = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
if (!$con) {
    $danger[] = mysqli_connect_error();
} else {
    //echo "It worked";

    // HopefulLlama code below:

    // Construct our SQL query, which will retrieve data from the database.
    $sql="SELECT * FROM Users";
    $result=mysqli_query($con,$sql);

    // Loop through all results return by our query
    while ($row = mysqli_fetch_assoc($result)) {
        // $row['rank'] will contain our Integer of 0, 1, 2, or 3
        echo $row['rank']

        // This will access your $rank array, and use the index retrieved from the database as an accessor.
        echo $rank[$row['rank']]
    }
}

【讨论】:

  • 这和我的回答有什么不同?
  • 不是,你刚刚在我打字的时候得到了答案。
  • @Baklap4 这个答案更清晰,更少混乱。它确实回答了这个问题。
  • 注意:未定义变量:在第 39 行的 C:\xampp\htdocs\includes\config.php 中排名
  • @KameronSchwab 你必须弄清楚你的等级存储在哪个变量中。您的问题中有 $rank 变量,但现在您说它未定义。它去哪儿了?可能是 $_SESSION['rank'] 会做。
【解决方案2】:

可以用sql语句查询数据库。您正在寻找的是用户表上的一个选择:

// array which holds the info
$rank = array ('user', 'moderator', 'administrator', 'owner');
// set up db connection
$con = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
if (!$con) {
    $danger[] = mysqli_connect_error();
} else { 
    //echo "It worked";

    // Baklap4's Addition:

    // SQL statement to retrieve the row where username is piet
    $sql = "SELECT * FROM Users WHERE username = `piet`";

    // Execute the query and check if there is a result.
    if ($result = $con->query($sql)) { 
        // if there are results loop through them.
        while ($row = mysqli_fetch_assoc($result)) {
            // print for each rank a different rank name.
            echo $rank[$row['rank']];
        } 
        // free resultset
        $result->close();
    } 
    // close connection
    $con->close();
    //clean up the temp variables 
    unset($obj); 
    unset($sql); 
    unset($result);
}

这将从您的数据库中检索用户Piet 并打印排名(在数据库中列出)。 然后它将取消设置在 whileloop 中生成的所有临时值,因此您不能再次重用它们。

您应该做的是将硬编码值piet 更改为变量。可以去http://localhost/scriptname.php?username=piet 检索相同的结果。

要使其成为变量,请按如下方式更改 sql 行:

$sql = "SELECT * FROM Users WHERE username = $_GET['username']";

【讨论】:

  • 我有点困惑。
  • 详细说明您的困惑之处。除非我们知道要帮助什么,否则我们无能为力。
  • 我要做的就是检测排名行中的 INT 是否 = 0、1、2 或 3,如果是,我希望它回复类似 2 =管理员(但只有管理员)
  • 解析错误:语法错误,意外 '' (T_ENCAPSED_AND_WHITESPACE),在 C:\xampp\htdocs\includes\config.php 中需要标识符 (T_STRING) 或变量 (T_VARIABLE) 或数字 (T_NUM_STRING)第 25 行
  • 另一个错误Fatal error: Uncaught Error: Call to a member function close() on boolean in C:\xampp\htdocs\includes\config.php:48 Stack trace: #0 {main} thrown in C:\xampp\htdocs\includes\config.php on line 48
猜你喜欢
  • 1970-01-01
  • 2012-01-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多