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

EpiDatabase #44

Open
rrasto opened this issue Sep 22, 2012 · 1 comment
Open

EpiDatabase #44

rrasto opened this issue Sep 22, 2012 · 1 comment

Comments

@rrasto
Copy link

rrasto commented Sep 22, 2012

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;
  }
}
@jmathai
Copy link
Owner

jmathai commented Oct 9, 2012

Thanks for this. I will review and merge this but it may take me a little bit :)

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