Last active
December 28, 2015 13:09
-
-
Save relipse/7505609 to your computer and use it in GitHub Desktop.
Upsert generator, (pdo style)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//This snippet is released to the public domain of course | |
/** | |
* Upsert one column value | |
*/ | |
function upsert($pkcolname, $pk, $table, $column, $val) { | |
$sql_upsert_templ = 'INSERT INTO `{TABLE}` (`{PKCOLUMN}`, `{COLUMN}`) VALUES (:pk, :value) | |
ON DUPLICATE KEY UPDATE `{COLUMN}` = :value_dup'; | |
$params = array(':pk'=>$pk, ':value'=>$val, ':value_dup'=>$val); | |
$sql = $sql_upsert_templ; | |
$sql = preg_replace('/\\{TABLE\\}/', $table, $sql); | |
$sql = preg_replace('/\\{COLUMN\\}/', $column, $sql); | |
$sql = preg_replace('/\\{PKCOLUMN\\}/', $pkcolname, $sql); | |
return array($sql, $params); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment