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

时间:2022-01-21 00:17:18

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

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

implode() function:passed invalid arguments

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 个解决方案

#1


13  

Try something like this:

尝试这样的事情:

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

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

将“UserID”替换为表中id的列名。

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

所以:首先构建数组,然后将数组内部转换为字符串。

#2


2  

There is my solution:

有我的解决方案:

SELECT GROUP_CONCAT(UserID) as string  FROM Users;

For this function the delimiter is ',' by default.

对于此函数,默认情况下分隔符为“,”。

#3


0  

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

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

the result 1,2,3...

结果1,2,3 ......

#1


13  

Try something like this:

尝试这样的事情:

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

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

将“UserID”替换为表中id的列名。

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

所以:首先构建数组,然后将数组内部转换为字符串。

#2


2  

There is my solution:

有我的解决方案:

SELECT GROUP_CONCAT(UserID) as string  FROM Users;

For this function the delimiter is ',' by default.

对于此函数,默认情况下分隔符为“,”。

#3


0  

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

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

the result 1,2,3...

结果1,2,3 ......