Php 受mysqli影响的行返回0,其中包含一个项目数组
我遇到了以下问题。 我有一个要更新的项目数组。下面是我的代码:Php 受mysqli影响的行返回0,其中包含一个项目数组,php,mysqli,Php,Mysqli,我遇到了以下问题。 我有一个要更新的项目数组。下面是我的代码: public function addTranslation( $data = array(), $language ) { // Add all words to language $stmt = $this->_db->prepare("UPDATE `languages` SET `$language` = ? WHERE `id` = ?"); $stmt->bind_param('
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->受影响的行
也适用于其他地方。我的问题是,我想检查是否所有填写的字段都已更新,但受影响的行始终返回0,即使字段已更新
这是否与我使用一个项目数组进行更新有关
我希望有人能解释一下这个问题。受影响的行只适用于最后的
执行
。试试这个:
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;
}
}
\u db->prepare
可能是您的问题,将其更改为$db->prepare
可能会有帮助\u db
实际上是mysqli连接$db
是我用来定义数据库类的东西,以每个执行语句的受影响行。在一个循环中执行不同的语句(绑定到不同的参数),并仅在之后检查受影响的_行。但是这里受影响的行总是1,除非您有重复的id
s。