Here is my code, I am using my login system to display certain content to certain account levels. The problem is that for some reason my code is not working. As you can see below I am querying:
SELECT rank FROM members WHERE username='$authUser'
Which is supposed to get the rank of the user that is logged in Via:
$_SESSION['username']
PHP
<?php
include "dbconf.php";
include "loginheader.php";
try {
$conn = new PDO("mysql:host=$host;dbname=$db_name", $username, $password);
//Trying to set PDO error mode to exception I guess?
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
//No Need to say if we connected successfully. // echo "Connected Successfully!";
$authUser = $_SESSION['username']; // get logged in user.
$result = $conn->query("SELECT rank FROM members WHERE username='$authUser'");
$userrank = $result->fetchAll(PDO::FETCH_OBJ);
}
catch(PDOException $e)
{
echo "Connection to database has failed: " . $e->getMessage();
}
function ShowRank($user) {
echo '<font color="green">' . $userrank[1] . '</font>';
}
?>
Now, where I am having trouble is that when I use:
var_dump($userrank);
What I get is an array that looks like this:
array(1) { [0]=> object(stdClass)#4 (1) { ["rank"]=> string(13) "Administrator" } }
So naturally I wrote:
echo $userrank[1];
However, for some reason it is returning empty. The output should be "Administrator" shouldn't it?