diff --git a/src/Modler/Collection/Mysql.php b/src/Modler/Collection/Mysql.php index e177bfb..9fb58f8 100644 --- a/src/Modler/Collection/Mysql.php +++ b/src/Modler/Collection/Mysql.php @@ -32,16 +32,32 @@ public function getDb() public function fetch($sql, array $data = array(), $single = false) { $sth = $this->getDb()->prepare($sql); + if ($this->isFailure($sth, $sth)) { + return false; + } + $result = $sth->execute($data); + if ($this->isFailure($sth, $result)) { + return false; + } + $results = $sth->fetchAll(\PDO::FETCH_ASSOC); + return ($single === true) ? array_shift($results) : $results; + } + + /** + * @param PDOStatement|boolean $sth + * @param mixed $result + * @return boolean TRUE if $result indicates failure, FALSE otherwise + */ + private function isFailure($sth, $result) + { if ($result === false) { $error = $sth->errorInfo(); $this->lastError = 'DB ERROR: ['.$sth->errorCode().'] '.$error[2]; error_log($this->lastError); - return false; + return true; } - - $results = $sth->fetchAll(\PDO::FETCH_ASSOC); - return ($single === true) ? array_shift($results) : $results; + return false; } -} \ No newline at end of file +}