$sql = "SELECT topicid FROM posttopic WHERE $id = :postid";
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':postid', $id);
$stmt->execute();
while ($topic = $stmt->fetch(PDO::FETCH_ASSOC)) {
$topicid = $topic['topicid'];
$sql = "SELECT name FROM topic WHERE $topicid = :id";
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':id', $topicid);
echo "<p>SQL: ".$sql."</p>";
$stmt->execute();
}
The first SELECT works fine. It's the second one that throws an exception.
I echo out the SQL statement and get...
SQL: SELECT name FROM topic WHERE 1 = :id
However, after the error is thrown, I get a SQL statement looking like...
SQL: SELECT name FROM topic WHERE = :id
Here's the obvious error message...
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '= NULL' at line 1'
I even do a var_dump before the SQL statement and it looks fine.
array(1) { ["topicid"]=> string(1) "1" } array(1) { ["name"]=> string(7) "General" }
I do one after the execute and suddenly the name part isn't there.
array(1) { ["topicid"]=> string(1) "1" }