从连接的查询字符串结果中删除尾随逗号
Remove trailing comma from concatenated query string result
希望你能帮上忙,在这里撕我的头发!
我有...
$result = mysqli_query($con,'SELECT this FROM that');
while($row = mysqli_fetch_array($result))
{
echo $row['this'] . ',';
}
哪个returns...
222,225,243,256,260,269,273,280,295,296,
我需要删除最后一个逗号,以便...
222,225,243,256,260,269,273,280,295,296
我已经尝试了 trim
rtrim
substr
和我能找到的所有其他方法,但其中 none 有效。认为这与 $row['this'] . ','
的串联有关,但我不知道如何解决它!
如有任何帮助,我们将不胜感激。
干杯,
马克.
让 MySQL 为您完成工作:
$result = mysqli_query($con, 'SELECT GROUP_CONCAT(this, ',') as thises FROM that');
这会将结果构造为逗号分隔的字符串。你可以通过它的名字来引用它,thises
.
使用 rtrim 删除最后一个逗号。
rtrim($my_string,',');
你可以使用 implode
while($row = mysqli_fetch_array($result))
{
$a[]= $row['this'];
}
$b = implode(',',$a);
print_r($b);
内爆函数
function imp($c){
$d= implode(',',$c);
return $d;
}
$b = imp($a);
print_r($b);
您可以使用 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)
string = 您生成的字符串
start = 您要从哪个位置开始字符串。
length = 正数 - 从起始参数返回的长度。负数 - 从字符串末尾返回的长度
希望你能帮上忙,在这里撕我的头发!
我有...
$result = mysqli_query($con,'SELECT this FROM that');
while($row = mysqli_fetch_array($result))
{
echo $row['this'] . ',';
}
哪个returns... 222,225,243,256,260,269,273,280,295,296,
我需要删除最后一个逗号,以便... 222,225,243,256,260,269,273,280,295,296
我已经尝试了 trim
rtrim
substr
和我能找到的所有其他方法,但其中 none 有效。认为这与 $row['this'] . ','
的串联有关,但我不知道如何解决它!
如有任何帮助,我们将不胜感激。
干杯, 马克.
让 MySQL 为您完成工作:
$result = mysqli_query($con, 'SELECT GROUP_CONCAT(this, ',') as thises FROM that');
这会将结果构造为逗号分隔的字符串。你可以通过它的名字来引用它,thises
.
使用 rtrim 删除最后一个逗号。
rtrim($my_string,',');
你可以使用 implode
while($row = mysqli_fetch_array($result))
{
$a[]= $row['this'];
}
$b = implode(',',$a);
print_r($b);
内爆函数
function imp($c){
$d= implode(',',$c);
return $d;
}
$b = imp($a);
print_r($b);
您可以使用 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)
string = 您生成的字符串
start = 您要从哪个位置开始字符串。
length = 正数 - 从起始参数返回的长度。负数 - 从字符串末尾返回的长度