删除最后一项的逗号。在 php 中使用 fwrite

Remove the comma at the last item. Using fwrite in php

您好,我正在尝试删除列最后一项的逗号

$return .= "INSERT INTO `".$table."` (";
while($column = $resultCol->fetch_assoc()) {
    $return .= "`".$column['col']."`, ";
}
$return .= ") VALUES ()";
rtrim($return, ", ");
fwrite($handle, $return);

但它不起作用。请帮忙

$return .= ") VALUES ()";
rtrim($return, ", ");

替换为:

$return .= rtrim($return, ", ");
$return .= ") VALUES ()";

while 循环完成后使用 rtrim 函数。 试试这个:

$return .= "INSERT INTO `".$table."` (";
while($column = $resultCol->fetch_assoc()) {
    $return .= "`".$column['col']."`, ";
}

$return .= rtrim($return,',');
$return .= ") VALUES ()";
rtrim($return, ", ");
fwrite($handle, $return);

试试这个。

$return .= "INSERT INTO `".$table."` (";
while($column = $resultCol->fetch_assoc()) {
    $return .= "`".$column['col']."`, ";
}
$return = rtrim($return, ", ").") VALUES ()";
fwrite($handle, $return);

你也可以试试这个 -

$return .= "INSERT INTO `".$table."` (";
$cols= array();
while($column = $resultCol->fetch_assoc()) {
    $cols[]= "`".$column['col']."`";
}
$return .= implode(',', $cols) . ") VALUES ()";