[英]mysqli affected rows returning 0 with an array of items
I came across the following problem. 我遇到以下问题。 I have an array of items that I want to update. 我有一系列要更新的项目。 Below is my code: 下面是我的代码:
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;
}
}
I know that the query works fine because when I checked my database after the script ran the updated values are there. 我知道查询工作正常,因为在脚本运行后检查数据库时,更新的值在那里。 Also $stmt->affected_rows
works in other places. $stmt->affected_rows
可以在其他地方使用。 The problem I have is that I want to check if all the filled in fields are updated but affected_rows
always returns 0 even when fields are updated. 我的问题是我想检查所有填写的字段是否都已更新,但是即使更新了字段, affected_rows
总是返回0。
Is this something to do with the fact that I update with an array of items? 这与我用一系列商品进行更新的事实有关吗?
I hope someone could shed some light on this problem. 我希望有人可以阐明这个问题。
The affected_rows
only applies to the last execute
. affected_rows
仅适用于最后一次execute
。 Try this: 尝试这个:
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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.