0

Here's my Function/Class

class user
{
    public function get_data($username)
    {
        global $pdo;
        $query = $pdo->prepare("SELECT * WHERE user_name = ?"); //display oldest to newest
        $query->bindValue(1, $username);
        $query->execute();
        return $query->fetch();
    }
}

Here's how I do my Call.

$users = new user;
$user = $users->get_data($_POST['username']);
$_SESSION['username'] = $user['user_name'];
$_SESSION['id'] = $user['user_id'];
$_SESSION['credits'] = $user['user_credits'];

Here's my HTML

<li class="right"><a href="#">Hello, <?php echo $_SESSION['username']; ?>.</a></li>
<li class="right"><?php echo $_SESSION['credits']; ?></li>
<li class="right"><?php echo $_SESSION['id']; ?></li>

So for some reason, only the $_SESSION['username']; print's out in the html, the credit/id doesn't have a value so it doesn't display anything, is my query right? I think it should work if the username is being displayed.

3
  • 2
    Shouldn't you first bind value and then execute? Commented Jun 19, 2013 at 7:42
  • Thanks for pointing that out everyone, but it still doesn't give me the values when I run the script, it just gives me the username. Commented Jun 19, 2013 at 7:47
  • Because your query is wrong, see my updated answer Commented Jun 19, 2013 at 7:48

2 Answers 2

3

execute before bindValue?

    $query->execute();
    $query->bindValue(1, $username);

Should be

    $query->bindValue(1, $username);
    $query->execute();

Also, Your Query is wrong

"SELECT * WHERE user_name = ?"   // SELECT from where?

Should be something like

"SELECT * FROM myTable WHERE user_name = ?"
Sign up to request clarification or add additional context in comments.

1 Comment

I forgot the FROM, I didn't even see that! Thank you so much for pointing that out!
0

Execute function should be call after bind value

$query->execute();
$query->bindValue(1, $username);

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.