AKrush95
AKrush95

Reputation: 1431

Prepare PDO sql statement with variable to call values on

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

Upvotes: 0

Views: 107

Answers (1)

Supericy
Supericy

Reputation: 5896

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

Upvotes: 2

Related Questions