0

Okay, so obviously the code I have now doesn't work

$stmt = $pdo->prepare('SELECT * FROM '.$table.' WHERE id = :p');
foreach($stmt->execute(array(':p' => $_GET['p'])) as $row) {
    echo $row['id'];
}

but, hopefully you can understand what I'm trying to do. I want to return values from an sql entry that are related to id whatever. I'm not exactly sure what combination of prepare, execute, or query I need to use to achieve this.

Thanks

1
  • 1
    execute() returns true or false, not an array. You just call $stmt->fetch(PDO::FETCH_ASSOC) in a while loop, or $stmt->fetchAll() to get an array. php.net/manual/en/pdostatement.fetch.php Commented Feb 6, 2013 at 1:58

1 Answer 1

2

Simplest answer:

$stmt = $pdo->prepare('SELECT * FROM ' . $table . ' WHERE id = :p');

$stmt->execute(array(':p' => $_GET['p']));

while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
    echo $row['id'];
}

I highly recommend taking a look at what each of these individual methods do:

http://php.net/manual/en/book.pdo.php

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

2 Comments

Thank you. I've been looking there but there's just so much stuff and so much to take in it got intimidating. Thanks again.
foreach ( $stmt as $row) is possible too, but after the execute() call

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.