问题描述
我正在设置一个具有多个用户角色的Web应用程序,这些角色确定特定用户在访问特定部分时所获得的视图(或这些部分是否可供他们使用).我有一个表(用户"),其中包含用户名"和角色"的列.我有第二个表(角色"),该表具有角色"列以及每个部分的列,每个表都有多个可能的值来驱动每个角色的用户体验.我在这里关注的列称为"useradminview",但其他所有列都存在相同的问题.
I'm setting up a web application that has multiple user roles that determine what view a specific user gets when visiting specific sections (or whether those sections are even available to them). I have a table ("users") that includes a columns for "username" and "role". I have a second table ("roles") that has columns for "role" as well as a column for each section, each having multiple possible values that drive the user experience for each role. The column I'm concerned with here is call "useradminview", but I have the same issue with all other columns.
我在登录时获得给定用户的角色没有问题.但是,当我尝试获取与该角色关联的useradmin视图时,我的查询返回的是列名而不是预期值.
I have no problem obtaining a given user's role when they login. But when I attempt to get the useradmin view associated with that role, my query returns the column name rather than the expected value.
我在stackoverflow和其他网站上发现了几处相同症状的帖子,但是查询的设置与我的设置有所不同.任何帮助,我们将不胜感激!
I've found several posts on stackoverflow and other sites that are for the same symptom, but the queries are setup differently from what I have. Any help is greatly appreciated!
//function to get role for user - this returns the expected value
function getUserRole($username) {
include($_SERVER["DOCUMENT_ROOT"] . "/sharedinc/db.php");
try {
$sql = "SELECT role FROM users WHERE username = :username";
$s = $pdoUsers->prepare($sql);
$s->bindValue(":username", $username);
$s->execute();
} catch (PDOException $e) {
$error = "Error obtaining user role";
include($_SERVER["DOCUMENT_ROOT"] . "/sharedinc/dboutput.php");
exit();
}
$role = $s->fetch();
return $role[0];
}
//function to check page view for that role - this returns the column name
function getPageView($role, $page) {
include($_SERVER["DOCUMENT_ROOT"] . "/sharedinc/db.php");
try {
$sql = "SELECT :page FROM roles WHERE role = :role";
$s = $pdoUsers->prepare($sql);
$s->bindValue(":role", $role);
$s->bindValue(":page", $page);
$s->execute();
} catch (PDOException $e) {
$error = "Error obtaining pageview for role";
include($_SERVER["DOCUMENT_ROOT"] . "/sharedinc/dboutput.php");
exit();
}
$pageview = $s->fetch();
return $pageview[0];
}
//end-goal query needs to be able to store the values as session variables
$_SESSION["username"] = $_POST["username"];
$_SESSION["role"] = getUserRole($_SESSION["username"]);
$_SESSION["useradminview"] = getPageView($_SESSION["role"], "useradminview");
推荐答案
您不能通过绑定输入行名.更改为:
you cant put row-name via bind. change it to:
try {
$sql = "SELECT $page FROM roles WHERE role = :role";
$s = $pdoUsers->prepare($sql);
$s->bindValue(":role", $role);
$s->execute();
} catch (PDOException $e) {
这篇关于PHP PDO mySQL查询返回列名而不是值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!