Skip to content

Instantly share code, notes, and snippets.

@tomislacker
Created March 26, 2014 13:40
Show Gist options
  • Save tomislacker/9783363 to your computer and use it in GitHub Desktop.
Save tomislacker/9783363 to your computer and use it in GitHub Desktop.
Using array_map to escape strings with closures and mysql_real_escape_string
<?php
$db = @mysql_connect(HOST, USER, PASS);
$newRecord = array(
'Name' => 'tomislacker',
'Email' => 'notfor@you.me'
);
$keys = array_map(
function ($k) use ($db) {
return @mysql_real_escape_string($k, $db);
},
array_keys($newRecord)
);
$vals = array_map(
function ($v) use ($db) {
return @mysql_real_escape_string($v, $db);
},
array_values($newRecord)
);
$sql = '
INSERT INTO
MyTable
('.implode(',', $keys).')
VALUES
("'.implode('","', $vals).'")
';
mysql_query($sql, $db);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment