whitelamp whitelamp - 1 year ago 65
MySQL Question

PHP-PDO: Display all rows

I'm doing var_dump since I started to learn PHP/PDO when I want to display the SQL datas, like:

$tickets = $db->query('SELECT * FROM tickets')->fetchAll();

And it shows everything.

But now I'd like to
a list of all tickets name, usually I would do an
echo $tickets->name;
but first it doesn't work with fetchAll(); and when I use fetch(); it returns only the last row

Noob question but all the answers I get are outdated and uses depreciated mysql, how can I just display the rows with PDO ?

Thanks !

Answer Source

PDO fetch_all default to fetching both numeric and associative arrays

$tickets = $db->query('SELECT * FROM tickets')->fetchAll();
foreach($tickets as $ticket) {
    echo $ticket['name'];