Created
January 8, 2012 21:16
-
-
Save qsun/1579727 to your computer and use it in GitHub Desktop.
Easier PDO
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
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