簡體   English   中英

php pdo mysql fetch返回一般錯誤?

[英]php pdo mysql fetch returns a general error?

嘗試這樣做時:

        // 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]:第68行/home/gamingon/public_html/prxa.info/includes/class_mysql.php中的常規錯誤

這是我的查詢和獲取功能:

// 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();**
}

在循環的第一次迭代期間,您將覆蓋$db實例的STH屬性。 換一種說法:

  1. 你做一個SELECT查詢, $db->STH是一個SELECT預處理語句。
  2. 您在SELECT語句上執行fetch()調用,這是有效的。
  3. 第一次循環迭代:使用UPDATE預處理語句覆蓋$db->STH
  4. 就在第二次循環迭代之前:你不能再進行fetch()調用,因為SELECT語句消失了,INSERT語句就在它的位置。

重構數據庫類或創建$db類的新實例(使用不同的變量名!)以在循環中使用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM