dongmuzhan4705 2014-05-08 15:20
浏览 32
已采纳

使用`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 ......

  • 写回答

2条回答 默认 最新

  • drmg17928 2014-05-08 15:23
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集