尝试这样做时:
// make unread notifications
$db->sqlquery("SELECT `participant_id` FROM `user_conversations_participants` WHERE `conversation_id` = ? AND `participant_id` != ?", array($_POST['conversation_id'], $_SESSION['user_id']));
while ($participants = $db->fetch())
{
$db->sqlquery("UPDATE `user_conversations_participants` SET `unread` = 1 WHERE `participant_id` = ?", array($participants['participant_id']));
}
我似乎得到了这个错误:
警告:PDOStatement :: fetch()[pdostatement.fetch]:SQLSTATE [HY000]: 一般错误 /home/gamingon/public_html/prxa.info/includes/class_mysql.php在线 68
这是我的查询和获取功能:
// the main sql query function
public function sqlquery($sql, $objects = array())
{
global $core;
try
{
$this->STH = $this->database->prepare($sql);
foreach($objects as $k=>$p)
{
// +1 is needed as arrays start at 0 where as ? placeholders start at 1 in PDO
if(is_numeric($p))
{
$this->STH->bindValue($k+1, (int)$p, PDO::PARAM_INT);
}
else
{
$this->STH->bindValue($k+1, $p, PDO::PARAM_STR);
}
}
$this->counter++;
return $this->STH->execute();
}
catch (Exception $e)
{
$core->message($e->getMessage());
}
}
public function fetch()
{
$this->STH->setFetchMode(PDO::FETCH_ASSOC);
**return $this->STH->fetch();**
}
答案 0 :(得分:4)
在循环的第一次迭代期间,您将覆盖STH
实例的$db
属性。换句话说:
$db->STH
是SELECT预处理语句。fetch()
调用,该语句有效。 $db->STH
。fetch()
调用,因为SELECT语句已经消失,INSERT语句就在其位置。重构数据库类或创建$db
类的新实例(使用不同的变量名!)以在循环中使用。