Skip to content

Instantly share code, notes, and snippets.

@troelskn
Created September 8, 2009 21:00
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 troelskn/183219 to your computer and use it in GitHub Desktop.
Save troelskn/183219 to your computer and use it in GitHub Desktop.
<?php
function mysql_bind($sql, $params = array()) {
$buffer = array();
$tokens = preg_split('/(:[a-z0-9_]+)/', $sql, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
foreach ($tokens as $token) {
if (preg_match('/:[a-z0-9_]+/', $token)) {
if (!array_key_exists($token, $params)) {
trigger_error("Missing parameter '$token'");
} elseif (!isset($params[$token])) {
$buffer[] = "null";
} else {
$buffer[] = '"'. mysql_escape_string($params[$token]) . '"';
}
} else {
$buffer[] = $token;
}
}
return implode($buffer, "");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment