我有一个SQL语法来代替

  $query = "REPLACE INTO ".TBL_ACTIVE_ADMINS." VALUES (:username, :time, :userlevel)";
  $stmt = $this->connection->prepare($query);
  $stmt->execute(array(':username' => $username, ':time' => $time, ':userlevel' => $userlevel));


这工作正常,但我想稍加调整,以便其中有一个where子句,仅允许:userlevel为'9'。

  $query = "REPLACE INTO ".TBL_ACTIVE_ADMINS."(:username, :time, :userlevel) SELECT * FROM ".TBL_USERS." WHERE userlevel = 9 SET username = :username and timestamp = :time and userlevel = :userlevel";
  $stmt = $this->connection->prepare($query);
  $stmt->execute(array(':username' => $username, ':time' => $time, ':userlevel' => $userlevel));


谁能向我解释以上代码中我做错了什么?

最佳答案

没关系;在进行了很多更改之后,我决定从不同的角度来看待它。现在,我已经编写了一个函数,该函数每次刷新页面时都会清理SQL表,因此可以准确统计当前在我的站点上活动的人员。

我使用下面的代码,然后在调用计数之前,我清理了数据库表

$query = "REPLACE INTO ".TBL_ACTIVE_ADMINS." VALUES (:username, :time, :userlevel)";
$stmt = $this->connection->prepare($query);
$stmt->execute(array(':username' => $username, ':time' => $time, ':userlevel' => $userlevel));


所以我的最终代码看起来像这样

$query = "REPLACE INTO ".TBL_ACTIVE_ADMINS." VALUES (:username, :time, :userlevel)";
$stmt = $this->connection->prepare($query);
$stmt->execute(array(':username' => $username, ':time' => $time, ':userlevel' => $userlevel));

$query = "DELETE FROM ".TBL_ACTIVE_ADMINS." WHERE userlevel = 3";
$stmt = $this->connection->prepare($query);
$stmt->execute(array(':userlevel' => $userlevel));

$this->calcNumActiveAdmins();

关于mysql - 替换为SQL语法错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22966296/

10-11 03:11