PHP PDO mySQL query returns column name instead of value

nicko

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.

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.

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");
Bernd Buffen

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) {

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

PDO query binding string instead of integer returns results

PHP PDO Select query returns double values

PHP returns element with name instead of ID

MySQL Query equals column name

Get column name instead of column value

MySQL PHP PDO Fetch all users with a value of Yes in column

Query to database is not returning row value, instead is returning column name

php $_POST variable as column name in mysql query

Failed to create temporary table Mysql Php Pdo (mysql.column_stats: expected column 'max_value' …)

My PHP PDO fetchAll() code returns one row instead of all results on MySql column table

Indirect expansion returns variable name instead of value

Query a mysql table for a dynamic column name and its value in python?

PHP PDO mySQL query returns column names and integers

mysql query returns two results instead of one

PDO query returns nothing with PHP 5.1.6

Mysql Query - Get row value based on column name in a different table

PDO query returns "Array" instead of value

Column name and value get PDO

mysql_query returns true instead Resource

MySql Query/PHP to get all column value with same customer name

Php, PDO query, fetch all returns nothing

Prolog returns variable name instead of value

mysqli query returns the column name as one row in php

mysql column returns & instead of &

MySQL returns the name of the column instead of the value

Increment value in a MySql query using PDO

MySQL query returns no results with PDO

Sql: Query returns column name instead of Value

Sqlite database returns nonexistant column name instead of exception due to bad query