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

时间:2015-12-10 06:41:43

标签: php mysql fwrite

您好我试图删除最后一列

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

但它不起作用。求助。

4 个答案:

答案 0 :(得分:2)

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

替换为:

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

答案 1 :(得分:0)

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

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

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

答案 2 :(得分:0)

试试这个。

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

答案 3 :(得分:0)

你也可以试试这个 -

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