Created
February 7, 2014 21:43
-
-
Save jshcrowthe/8872520 to your computer and use it in GitHub Desktop.
PHP PDO Database Query
This file contains hidden or 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
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