Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
210 views
in Technique[技术] by (71.8m points)

php - fetchAll isn't returning correct information

Where have I gone wrong with this code? I thought the print_r($stmt); would return u_id from the users table however it is just returning the prepared statement.

if(isset($_GET['email']) && isset($_GET['token'])) {
  global $pdo;

  $email = $_GET['email'];
  $token = $_GET['token'];

  $sql = ("SELECT u_id FROM users WHERE
    email = ? AND
    token = ? AND
    token<>'' AND
    tokenExpire > NOW()
    ");
  $stmt = $pdo->prepare($sql);
  $stmt->execute([$email, $token]);
  $stmt->fetchAll();
  print_r($stmt);

Thanks

question from:https://stackoverflow.com/questions/65906168/fetchall-isnt-returning-correct-information

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This is expected behaviour. The fetchAll method returns an array of results; but you haven't assigned it to a variable and so that line of code does nothing good for you.

However, if you were to do:

print_r($stmt->fetchAll());

Then you would have the result printed to screen; if you wanted to use the result afterwards, then you need to assign the returned value to a variable:

$result = $stmt->fetchAll();

print_r($result);

This assigns the result to a variable and then prints out the full set of results.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...