我有......
$result = mysqli_query($con,'SELECT this FROM that');
while($row = mysqli_fetch_array($result))
{
echo $row['this'] . ',';
}
返回... 222,225,243,256,260,269,273,280,295,296,
我需要删除最后一个逗号才能给我... 222,225,243,256,260,269,273,280,295,296
我已经尝试了trim
rtrim
substr
以及我能找到的所有其他内容,但其中没有一项可行。认为它与$row['this'] . ','
的串联有关,但我无法弄清楚如何解决它!
任何帮助都将不胜感激。
干杯, 标记
答案 0 :(得分:1)
让MySQL为您完成工作:
$result = mysqli_query($con, 'SELECT GROUP_CONCAT(this, ',') as thises FROM that');
这将结果构造为逗号分隔的字符串。您可以通过名称thises
来引用它。
答案 1 :(得分:1)
使用rtrim删除最后一个逗号。
3 is the same as 0011 in binary, and inverse of that is 1100
2 is the same as 0010 in binary, and inverse of that is 1101
5 is the same as 0101 in binary, and inverse of that is 1010
10 is the same as 1010 in binary.
10 & ~3 => 1010 & 1100 = 1000 => 8
10 & ~5 => 1010 & 1010 = 1010 => 10
10 & ~2 => 1010 & 1101 = 1000 => 8
答案 2 :(得分:0)
你可以使用implode
rtrim($my_string,',');
implode的功能
while($row = mysqli_fetch_array($result))
{
$a[]= $row['this'];
}
$b = implode(',',$a);
print_r($b);
答案 3 :(得分:0)
您可以使用 substr()
这是您的代码
$result = mysqli_query($con,'SELECT this FROM that');
while($row = mysqli_fetch_array($result))
{
echo $row['this'] . ',';
}
代码应该是这样的
$result = mysqli_query($con,'SELECT this FROM that');
$my_string = "";
while($row = mysqli_fetch_array($result))
{
$my_string .= $row['this'] . ',';
}
$my_final_string = substr($my_string, 0, strlen($my_string)-1);
echo $my_final_string;
<强>解释强>
substr(string,start,length)
字符串 =您生成的字符串
开始 =您要从哪个位置开始字符串。
长度 =正数 - 从start参数返回的长度。负数 - 从字符串末尾返回的长度