使用`fetchAll(“select`name` from”classes`“)创建的数组的回声值

I'm trying to echo an array value from an array that was created using the following code. The data for the array is pulled from a MySQL table.

$names = $db->fetchAll("select `name` from `classes`");

This is what is stored in the $names variable

Array ( [0] => Array ( [name] => Web Design ) [1] => Array ( [name] => Art History ) [2] => Array ( [name] => Gym ) [3] => Array ( [name] => English ) [4] => Array ( [name] => Biology ) [5] => Array ( [name] => 3D Animation ) [6] => Array ( [name] => Tech Disc ) [7] => Array ( [name] => Math ) [8] => Array ( [name] => Dance ) [9] => Array ( [name] => Video Production ) [10] => Array ( [name] => Home Ec ) [11] => Array ( [name] => Government ) [12] => Array ( [name] => Physics ) )

I'm attempting to echo a [name] value OR all [name] values, but I can't figure it out. I've tried the following....

<?php echo $names['name'];?>

returns nothing

<?php echo $names['0'];?> //AND\\ <?php echo $names[0];?>

Both return the string Array

Can someone please help me echo a single value from the array?

Example: Web Design or Art History

Also can someone please help me echo all the values from the array?

Example: Web Design Art History Gym English Biology ......

You should try <?php echo $names[0]['name'];?>, <?php echo $names[1]['name'];?>, etc....

This is because your query function returns an array with all results in it. In that array, each row returned is an array again. And each field is a key in that array.

Echoing a single value:

echo $names[0]['name'];

Echoing all values:

foreach ($names as $name) {
   echo $name['name'].' ';
}