我想检查用户用户的等级,该数据以0,1,2或3的整数存储在我的数据库中。
我想让它回声如同echo $ my_rank; #Instead of the say 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";
}
?>
答案 0 :(得分: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']]
}
}
答案 1 :(得分:0)
可以使用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
并打印排名(在数据库中列出)。
然后它会取消设置while循环中所有临时值,这样你就无法重复使用它们。
您应该做的是将硬编码值piet
更改为变量。可以去http://localhost/scriptname.php?username=piet
检索相同的结果。
要使这个变量变为sql行,如下所示:
$sql = "SELECT * FROM Users WHERE username = $_GET['username']";