将数组内存到mysql查询中的逗号分隔字符串中

时间:2012-07-22 11:32:29

标签: php mysql arrays string implode

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

  

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

$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;
}

3 个答案:

答案 0 :(得分:14)

尝试这样的事情:

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

"UserID"替换为表格中id的列名。

所以:首先构建数组,然后将数组爆炸成字符串。

答案 1 :(得分:2)

有我的解决方案:

{{1}}

对于此功能,分隔符是','默认情况下。

答案 2 :(得分:0)

$query = 'SELECT id FROM your_table';
$rs = mysql_query($query);

$row = mysql_fetch_array($result);
return implode(',', $row);

结果1,2,3 ...

相关问题