ds355020 2013-08-07 08:16
浏览 32
已采纳

将一些记录转换为逗号分隔的数组

I have table like this:

table Database
+------+--------------+
| name | data         |
+------+--------------+
| foo  | certaindata1 |
| bar  | certaindata2 |
| foo  | certaindata3 |
+------+--------------+

and I want to output data for array values like: array('certaindata1','certaindata2','certaindata3')

I have try this

$sql = mysql_query("SELECT data FROM Database");
while($row=mysql_fetch_array($sql)){
     echo $row['data'];
}

and result is certaindata1certaindata2certaindata3
where array value should be 'certaindata1','certaindata2','certaindata3'

  • 写回答

2条回答 默认 最新

  • dongxiangshen7916 2013-08-07 08:22
    关注

    The result is expected. You're simply echoing $row['data']; without any spaces or newlines -- so the result will be certaindata1certaindata2certaindata3. If you want to store the results in an array, you can do the following:

    while($row=mysql_fetch_array($sql)){
         $yourArray[] = $row['data']; //storing the $row['data'] in an array
    }
    $comma_separated = implode(',', $yourArray);
    print_r($comma_separated);
    

    Hope this helps!

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

报告相同问题?

悬赏问题

  • ¥15 用三极管设计—个共射极放大电路
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示