0

I'm very new to PDO. The issue I'm currently facing is that I'm trying to grab a users first name from a column however I have no idea how to fetch it properly. I've managed to find verify the user email with the email in the database now I just need to fetch the UserFirstName column and store it in a variable.

$data = array();
 if(isset($_POST['email'])){
  $data = $_POST['email'];


            if (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) === false) {
                $data = $_POST['email'];

            $stmt = $pdo->prepare('SELECT * FROM users WHERE UserEmail = :UserEmail');
                $stmt->execute(array(':UserEmail' => $data));
6
  • 1
    So you're basically asking how to fetch a row with PDO? Commented Jan 16, 2017 at 15:49
  • $stmt->execute( should be $result = $stmt->execute( and var_dump($result); Commented Jan 16, 2017 at 15:50
  • @samayo it would rather make no sense to do that Commented Jan 16, 2017 at 15:51
  • It does if the query is right Commented Jan 16, 2017 at 15:52
  • @samayo check the linked question Commented Jan 16, 2017 at 15:53

1 Answer 1

-1

change query to:

$stmt = $pdo->prepare('SELECT UserFirstName FROM users WHERE UserEmail = :UserEmail');

To execute:

$stmt->execute(array(':UserEmail' => $data));

then to fetch single column

$result = $stmt ->fetchColumn();
print("name = $result\n");

for reference http://php.net/manual/en/pdostatement.fetchcolumn.php

Sign up to request clarification or add additional context in comments.

3 Comments

It returns Invalid parameter number: no parameters were bound in "$result = $stmt ->fetchColumn();"
That's because you need to bind the parameters, use the execute you already had in the question: $stmt->execute(array(':UserEmail' => $data));
Oh I see now! Thank you for clearing that up and taking your time!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.