Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failed prepare() calls aren't checked in Collection\Mysql #7

Closed
elazar opened this issue May 23, 2016 · 1 comment
Closed

Failed prepare() calls aren't checked in Collection\Mysql #7

elazar opened this issue May 23, 2016 · 1 comment

Comments

@elazar
Copy link
Contributor

elazar commented May 23, 2016

It's possible for the prepare() call on this line to fail and return a boolean value of false (e.g. if the database is missing the relevant table), but there's no check for this (e.g. one equivalent to the one for the execute() call below it).

Here's an example of a refactor that might add this check:

/**
 * Fetch the data matching the results of the SQL operation
 *
 * @param string $sql SQL statement
 * @param array $data Data to use in fetch operation
 * @param boolean $single Only fetch a single record
 * @return array Fetched data
 */
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 true;
    }
    return false;
}
@enygma
Copy link
Owner

enygma commented May 25, 2016

Looks good to me - mind dropping that into a PR?

@enygma enygma closed this as completed in e933be4 May 25, 2016
enygma added a commit that referenced this issue May 25, 2016
Fix #7: Handle prepare() failure in Mysql collection
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants