Created
March 26, 2014 13:40
-
-
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
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 | |
$db = @mysql_connect(HOST, USER, PASS); | |
$newRecord = array( | |
'Name' => 'tomislacker', | |
'Email' => '[email protected]' | |
); | |
$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