I haven't been using #PDO and #MySQL for a very long time, so things are bound to pop up from time to time that confuse me a little. While developing the system for storing #hashtags in a table, I tried my hand at using a MySQL variable for the first time. In hindsight, it looks as though it wasn't necessary, but it's good practice anyway.
So, I wrote a statement that looked like this:
$stmt = $dbh->prepare('
INSERT INTO blog_posts (title, safe_title, body, created, author) VALUES (?, ?, ?, NOW(), ?);
SELECT LAST_INSERT_ID() INTO @post_id;
$hashtags['tag_insert'] is another insert statement generated by the function that parses for tags in the post. It uses the @post_id variable. So, I'm not actually selecting anything in this statement, other than into the variable. But, trying to get the LAST_INSERT_ID() from this statement, either by adding an additional line to SELECT it, or by SELECTing it with another statement, does not work.
If you SELECT it with the same statement, fetch() doesn't return anything, but if you create a new statement, you get an error saying you "Cannot execute queries while other unbuffered queries are active" and that you should call fetchAll(). But there's nothing to fetch, so it doesn't work. So we need to deactivate that query some other way.
The solution is to use $stmt->closeCursor();. Whatever PDO thinks you might want to do with the statement, it just drops it. Now I can create another statement for the LAST_INSERT_ID(). And it still remembers the value of the variable @post_id, so I can just use that.
Here's a link to the Stackoverflow question that I read to solve my problem.
Weird. But I imagine it only seems that way because there's some insight into MySQL that I'm currently missing.