Skip to content

Instantly share code, notes, and snippets.

@yuksbg
Created August 10, 2011 06:09
Show Gist options
  • Save yuksbg/1136233 to your computer and use it in GitHub Desktop.
Save yuksbg/1136233 to your computer and use it in GitHub Desktop.
php_array_to_sql
function arrayToSql($table, $array, $insert = "INSERT INTO") {
if ($insert != "UPDATE") {
$insert = "INSERT INTO";
}
$columns = array ();
$data = array ();
foreach ( $array as $key => $value ) {
$columns [] = $key;
if ($value != "") {
$data [] = "'" . $value . "'";
} else {
$data [] = "NULL";
}
}
$cols = implode ( ",", $columns );
$values = implode ( ",", $data );
$sql = <<<EOSQL
$insert `$table`
($cols)
VALUES
($values)
EOSQL;
return $sql;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment