mysql受影响的行返回0的项目数组


mysqli affected rows returning 0 with an array of items

我遇到了以下问题。我有一个想要更新的项数组。下面是我的代码:

public function addTranslation( $data = array(), $language ) {
    // Add all words to language
    $stmt = $this->_db->prepare("UPDATE `languages` SET `$language` = ? WHERE `id` = ?");
    $stmt->bind_param('si', $translation, $id);
    $count = 0;
    foreach( $data as $id => $translation ) {
        $stmt->execute();
        if( !empty( $translation ) ) {
            $count++;
        }
    }
    // Check if all filled in words are added
    echo $stmt->affected_rows;
    if( $stmt->affected_rows >= $count ) {
        return true;
    } else {
        return false;
    }
}

我知道查询工作得很好,因为当我检查我的数据库后,脚本运行更新的值在那里。$stmt->affected_rows在其他地方也有作用。我遇到的问题是,我想检查是否所有填写的字段都更新了,但affected_rows总是返回0,即使字段更新了。

这是否与我使用项目数组更新的事实有关?

我希望有人能说明这个问题。

affected_rows只适用于最后一个execute。试试这个:

public function addTranslation( $data = array(), $language ) {
    // Add all words to language
    $stmt = $this->_db->prepare("UPDATE `languages` SET `$language` = ? WHERE `id` = ?");
    $stmt->bind_param('si', $translation, $id);
    $count = 0;
    $affected = 0;
    foreach( $data as $id => $translation ) {
        $stmt->execute();
        $affected += $stmt->affected_rows;
        if( !empty( $translation ) ) {
            $count++;
        }
    }
    // Check if all filled in words are added
    echo $affected;
    if( $affected >= $count ) {
        return true;
    } else {
        return false;
    }
}