How do you fetch one single record from a MySQL database using a PDO prepared statement?
In the “old” way of doing it, I could use:
$MyCaption = mysql_result(mysql_query("SELECT * FROM `photos` WHERE `id` = '56465843362'"),0,"caption");
What would be the PDO prepared statement equivalent?
I tried:
$id = '56465843362';
$query = $db->prepare("SELECT `caption` FROM `photos` WHERE `id` = ?");
$query->execute(array($id));
$caption = $query->fetchColumn();
… but that was wrong.
The Limit keyword limits to the # of rows you want. But without an order by clause, your record would be randomly fetched. If it seems to be working correctly, do not assume it would behave so always. The SQL standard does not guarantee the order of the rows fetched by default.