Skip to content
This repository was archived by the owner on Mar 4, 2026. It is now read-only.
This repository was archived by the owner on Mar 4, 2026. It is now read-only.

EpiDatabase #44

Description

@rrasto

If you execute an SQL statement like this: INSERT INTO table (a,b,c) VALUES (1,2,3) ON DUPLICATE KEY UPDATE c=c+1; Your original solution contains the following error. If you try to insert a duplicate key, the INSERT statement is not executed and the SQL statement returns 0. But the subsequent UPDATE changed some rows, so one should expect that the function returns the number of updated rows.

public function execute($sql = false, $params = array())
{
  $this->init();
  try
  {
    $sth = $this->prepare($sql, $params);

    /* original solution
    if(preg_match('/insert/i', $sql))
      return $this->dbh->lastInsertId();
    else
      return $sth->rowCount();*/

    /* my quick fix */
    return ($this->dbh->lastInsertId() > $sth->rowCount()) ? $this->dbh->lastInsertId() : $sth->rowCount();
  }
  catch(PDOException $e)
  {
    EpiException::raise(new EpiDatabaseQueryException("Query error: {$e->getMessage()} - {$sql}"));
    return false;
  }
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions