Skip to content

Instantly share code, notes, and snippets.

@qsun
Created January 8, 2012 21:16
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 qsun/1579727 to your computer and use it in GitHub Desktop.
Save qsun/1579727 to your computer and use it in GitHub Desktop.
Easier PDO
public function executeSQL($dbh, $sql, $params = array()) {
try {
$stmt = $dbh->prepare($sql); //Used by all query types.
if (substr($sql, 0, 6) === "SELECT") {
//Select
$stmt->execute($params);
return $stmt->fetchAll(PDO::FETCH_ASSOC); //Returns an Array if data returned. Use is_array to check.
} else {
//Create/Insert/Update/Delete/Drop
return $stmt->execute($params); //Returns true/false
}
} catch(PDOException $e) {
trigger_error('An error has occured in the executeSQL Function.
Error acquiring data: ' . $e->getMessage() .
(isset($stmt) ? 'Debug Params: ' . $stmt->debugDumpParams() : ''),
E_USER_ERROR);
exit(0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment