Search code examples
phpmysqlfwrite

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


hi i'm trying to remove the comma at the last item of columns

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

but it doesn't work. help please.


Solution

  • You can also try this -

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