使用mysqli的排名系统
Ranking system using mysqli
我想检查用户的等级是多少,它以整数 0、1、2 或 3 的形式存储在我的数据库中。
我希望它像 echo $my_rank 那样被回显; #与其说 0、1、2 或 3,不如说 User 代表 0,Moderator 代表 1,Admin 代表 2,Owner 代表 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";
}
?>
可以使用 sql 语句查询数据库。您正在寻找的是用户表上的 select:
// 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
并打印排名(在数据库中列出)。
然后它将取消设置在 while 循环中创建的所有临时值,因此您不能再次使用它们。
您应该做的是将硬编码值 piet
更改为变量。人们可以去 http://localhost/scriptname.php?username=piet
检索相同的结果。
要使其成为变量,请按如下方式更改 sql 行:
$sql = "SELECT * FROM Users WHERE username = $_GET['username']";
嗯...根据你给我的信息,似乎只需要一个查询,从数据库中获取数据。然后我们就可以回显了。
在您的数据库连接的 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']]
}
}
我想检查用户的等级是多少,它以整数 0、1、2 或 3 的形式存储在我的数据库中。
我希望它像 echo $my_rank 那样被回显; #与其说 0、1、2 或 3,不如说 User 代表 0,Moderator 代表 1,Admin 代表 2,Owner 代表 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";
}
?>
可以使用 sql 语句查询数据库。您正在寻找的是用户表上的 select:
// 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
并打印排名(在数据库中列出)。
然后它将取消设置在 while 循环中创建的所有临时值,因此您不能再次使用它们。
您应该做的是将硬编码值 piet
更改为变量。人们可以去 http://localhost/scriptname.php?username=piet
检索相同的结果。
要使其成为变量,请按如下方式更改 sql 行:
$sql = "SELECT * FROM Users WHERE username = $_GET['username']";
嗯...根据你给我的信息,似乎只需要一个查询,从数据库中获取数据。然后我们就可以回显了。
在您的数据库连接的 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']]
}
}