Skip to content

Instantly share code, notes, and snippets.

@jshcrowthe
Created February 7, 2014 21:43
Show Gist options
  • Save jshcrowthe/8872520 to your computer and use it in GitHub Desktop.
Save jshcrowthe/8872520 to your computer and use it in GitHub Desktop.
PHP PDO Database Query
function dbQuery($conn, $sql='SELECT *', $params) {
$queryTerms = explode(" ", $sql);
$toBind = array();
foreach ($queryTerms as $term) {
if ($term[0] == ':') {
array_push($toBind, $term);
}
}
try {
$stmt = $conn->prepare($sql);
for ($i = 0; $i < count($toBind); $i++) {
$stmt->bindValue($toBind[$i], $params[$i], PDO::PARAM_STR);
}
$stmt->execute();
$results = $stmt->fetchAll();
$stmt->closeCursor();
} catch (PDOException $ex) {
$errormessage = 'Sorry, there was an error with the database.';
return false;
}
switch ($queryTerms[0]) {
case 'SELECT':
return $results;
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment