I'm playing around with PDO trying to really understand it. I have a small piece of code that selects all from a table, then displays the results on page. This works fine:
$query = $connection->query('select * from stats');
while ($row = $query->fetch()) {
echo $row['display_name'] . ' - ' . $row['short_name'] . '<br>';
}
This works as expected, returning the following to the page:
Connected to the database.
Magic - mag
Attack - atk
Defence - def
The only code before this is a try...catch
connecting to the database (creating a new object $connection
) - that's where the 'Connected to the database' line is coming from. If I try the following in a function, I see nothing on page:
function listAllStats() {
$query = $connection->query('select * from stats');
while ($row = $query->fetch()) {
echo 'From my function: ' . $row['display_name'];
}
};
listAllStats();
Any ideas what I'm doing wrong?