Skip to content

Instantly share code, notes, and snippets.

@chonthu
Created February 23, 2012 16:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chonthu/1893479 to your computer and use it in GitHub Desktop.
Save chonthu/1893479 to your computer and use it in GitHub Desktop.
on_duplicate_update 2
INSERT INTO
`product`
SET
`id`=?
,`name`=?
,`created_at`=NOW()
ON DUPLICATE KEY UPDATE
,`name`=?
';
$stmt = mysqli_prepare($dbh, $sql);
foreach ($products as $p) {
mysqli_stmt_bind_param($stmt, 'ss'.'ss',
$p['id']
,$p['name']
,$p['name']
);
$success = mysqli_stmt_execute($stmt);
$err = mysqli_error($dbh);
if (!$success && strpos($err, 'Duplicate entry')) {
echo "Error inserting product: ";
echo $err;
echo PHP_EOL;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment