Skip to content

Commit

Permalink
Merge pull request #8 from elazar/hotfix/mysql-collection-prepare
Browse files Browse the repository at this point in the history
Fix #7: Handle prepare() failure in Mysql collection
  • Loading branch information
enygma committed May 25, 2016
2 parents bcc0dbe + e933be4 commit bf7f5e6
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions src/Modler/Collection/Mysql.php
Expand Up @@ -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;
}
}
}

0 comments on commit bf7f5e6

Please sign in to comment.