我有一个名为“users”的表,其中包含“username”,“password”和“permission”列。在权限列中可以是“浏览”或“编辑”。 假设我有一个用户登录到我的网站,我想选择使用他们的用户名(我已存储在会话变量中)选择他们的权限。我想根据他们的权限设置一个等于'browse'或'edit'的变量,然后再用于进一步的逻辑。
假设我已连接并选择了相应的数据库,我很确定php代码和查询应该是这样的:
$u = $_SESSION['username'] ;
$sql = "SELECT permission FROM users WHERE username = '$u' " ;
$result = mysqli_query($sql);
但我不确定如何设置一个等于'浏览'或'编辑'的变量。
有什么想法吗?
答案 0 :(得分:1)
假设你有一个连接$con
,使用会话你必须开始你的会话。
$u = $_SESSION['username'] ;
$sql = "SELECT `permission` FROM `users` WHERE username='$u'";
$result = mysqli_query($con, $sql);
$rows = mysqli_fetch_object($result);
//now its time to set the permission to the variable
echo $permission = $rows->permission;
mysqli_close($con);
您也可以将$permission
设置为$_SESSION
。
$_SESSION['permission'] = $permission;
答案 1 :(得分:0)
你必须这样做:
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$u = $_SESSION['username'] ;
$sql = "SELECT `permission` FROM `users` WHERE username='$u'";
$result = mysqli_query($conn, $sql);
$rows = mysqli_fetch_object($result);
//now its time to set the permission to the variable
echo $permission = $rows->permission;
mysqli_close($conn);