Skip to content

Instantly share code, notes, and snippets.

@jagroop jagroop/Database.php
Created Nov 27, 2017

Embed
What would you like to do?
insert or update if duplicate (Bulk)
public static function insertOrUpdate($table, array $rows)
{
$first = reset($rows);
$columns = implode(',', array_map(function($value)
{
return "$value";
}, array_keys($first)));
$values = implode(',', array_map(function($row)
{
return '(' . implode(',', array_map(function($value)
{
return '"' . str_replace('"', '""', $value) . '"';
}, $row)) . ')';
}, $rows));
$updates = implode(',', array_map(function($value)
{
return "$value = VALUES($value)";
}, array_keys($first)));
$sql = "INSERT INTO {$table}({$columns}) VALUES {$values} ON DUPLICATE KEY UPDATE {$updates}";
return mysql_query($sql);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.