从MySQL查询将数组内嵌到以逗号分隔的字符串中 [英] implode an array into a comma separated string from mysql query

查看:486
本文介绍了从MySQL查询将数组内嵌到以逗号分隔的字符串中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在过去的1 1/2天里,我一直试图将16行ID存储到字符串中,并用逗号分隔每个ID.我得到的数组来自MySQL.我收到的错误是

For the last 1 1/2 days I've been trying to store 16 row id's into a string and separate each id with a comma. The array I am getting is from MySQL. The error I am getting is

implode()函数:传递了无效的参数

implode() function:passed invalid arguments

$str=array();
$string="";
while($row = mysql_fetch_row($result)) 
{
    $user_id=$row;
    $str=$user_id;
    foreach($str as $p=>$v){
        comma($v);
    }
}

function comma($v){
    $string= implode(",",$v); echo $string;
}

推荐答案

尝试如下操作:

$ids = array(); 
while ($row = mysql_fetch_assoc($result))  
{
    $ids[] = $row["UserID"]; 
} 
echo implode(", ", $ids);

用表中ID的列名替换"UserID".

Replace "UserID" with the columnname of the id in your table.

因此:首先构建数组,然后将数组内嵌到字符串中.

So: first you build the array, next you implode the array into a string.

这篇关于从MySQL查询将数组内嵌到以逗号分隔的字符串中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆