clean up database API for completeness and sensibleness
This commit is contained in:
parent
6b557983c4
commit
8b2e3262fe
@ -313,31 +313,61 @@ class Database {
|
|||||||
* Execute an SQL query and return an PDO resultset
|
* Execute an SQL query and return an PDO resultset
|
||||||
*/
|
*/
|
||||||
public function execute($query, $args=array()) {
|
public function execute($query, $args=array()) {
|
||||||
$result = $this->db->query($query, $args);
|
try {
|
||||||
return $result;
|
$stmt = $this->db->prepare($query);
|
||||||
|
foreach($args as $name=>$value) {
|
||||||
|
if(is_numeric($value)) {
|
||||||
|
$stmt->bindValue(":$name", $value, PDO::PARAM_INT);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$stmt->bindValue(":$name", $value, PDO::PARAM_STR);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$stmt->execute();
|
||||||
|
return $stmt;
|
||||||
|
}
|
||||||
|
catch(PDOException $pdoe) {
|
||||||
|
print "Message: ".$pdoe->getMessage();
|
||||||
|
print "<p>Error: $query";
|
||||||
|
exit;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Execute an SQL query and return a 2D array
|
* Execute an SQL query and return a 2D array
|
||||||
*/
|
*/
|
||||||
public function get_all($query, $args=array()) {
|
public function get_all($query, $args=array()) {
|
||||||
$result = $this->db->query($query, $args)->fetchAll();
|
return $this->execute($query, $args)->fetchAll();
|
||||||
return $result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Execute an SQL query and return a single row
|
* Execute an SQL query and return a single row
|
||||||
*/
|
*/
|
||||||
public function get_row($query, $args=array()) {
|
public function get_row($query, $args=array()) {
|
||||||
$result = $this->db->query($query, $args)->fetchAll();
|
return $this->execute($query, $args)->fetch();
|
||||||
if(count($result) == 0) {
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
return $result[0];
|
/**
|
||||||
|
* Execute an SQL query and return the first column of each row
|
||||||
|
*/
|
||||||
|
public function get_col($query, $args=array()) {
|
||||||
|
$stmt = $this->execute($query, $args);
|
||||||
|
$res = array();
|
||||||
|
foreach($stmt as $row) {
|
||||||
|
$res[] = $row[0];
|
||||||
}
|
}
|
||||||
|
return $res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute an SQL query and return a single value
|
||||||
|
*/
|
||||||
|
public function get_one($query, $args=array()) {
|
||||||
|
$row = $this->execute($query, $args)->fetch();
|
||||||
|
return $row[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a table from pseudo-SQL
|
* Create a table from pseudo-SQL
|
||||||
*/
|
*/
|
||||||
|
Loading…
x
Reference in New Issue
Block a user