0

Im using the following to log my users in,

/*** select the users name from the database ***/
    $dbh = new PDO("mysql:host=$mysql_hostname;dbname=$mysql_dbname", $mysql_username, $mysql_password);

    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $stmt = $dbh->prepare("SELECT * FROM access_users 
    WHERE id = :phpro_user_id");

    $stmt->bindParam(':phpro_user_id', $_SESSION['user_id'], PDO::PARAM_INT);

    $stmt->execute();

    $username = $stmt->fetchColumn();

    if($username == false)
    {
    $message = 'Access Error';
    }
    else
    {
        // done
    }

I want to retrieve the users level value, which is a column in my table only im unsure how to do this with PDO?

I've tried...

print $result->level;
2
  • 1
    Did you tried $username['level']? Cuz you save the result into $username. Commented Aug 19, 2013 at 11:05
  • 1
    Your code is extremely inconsistent. where did you get that $result? Why do you want level in a $username variable? Commented Aug 19, 2013 at 11:05

2 Answers 2

2

As it's impossible to get from your question, what column you ant to retrieve, assuming it is called "username":

$stmt = $dbh->prepare("SELECT username FROM access_users WHERE id = ?");
$stmt->execute(array($_SESSION['user_id']));
$username = $stmt->fetchColumn();

This is how fetchColumn() works.

But if you want to get all the user info, and among it, level and username, you have to retrieve usual way, a whole row.

$stmt = $dbh->prepare("SELECT * FROM access_users WHERE id = ?");
$stmt->execute(array($_SESSION['user_id']));
$row = $stmt->fetch();
if(!$row['level'])
{
    $message = 'Access Error';
}
echo "Hello ".$row['username'];
Sign up to request clarification or add additional context in comments.

Comments

0

Try the following:

$stmt = $dbh->prepare("SELECT user_id,level FROM access_users 
    WHERE id = :phpro_user_id");

//rest of the code up until here

$result = $stmt->fetchColumn(1);

print("level= $result\n");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.