id options
1 Website,Website,Newspaper,Newspaper,TV,TV,Radio,Radio
2 Website,Website,Newspaper,Newspaper
3 Website,Website,TV,TV
目标是删除重复的条目并将选项列标准化为:
id options
1 Website,Newspaper,TV,Radio
2 Website,Newspaper
3 Website,TV
$sql = "SELECT id, options FROM table";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$id = $row['id'];
$values_array = explode( ',' , $row['options'] );
if(count($values_array) != count(array_unique($values_array)))
{
$likes = array_unique($values_array);
$new = implode(',', $likes);
$sql = "UPDATE table SET options=".$new." WHERE id = '$id'";
}
}
} else {
echo "0 results";
}
$conn->close();
这不能完成工作.一切似乎都有效,但尝试使用新的数组数据更新选项列.
这似乎并不太难,只是寻找一些如何使其工作的指导.
提前致谢!
解决方法:
试试这个:
$sql = "SELECT id, options FROM table";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while ($row = $result->fetch_assoc()) {
$id = $row['id'];
$values_array = explode(',', $row['options']);
if (count($values_array) != count(array_unique($values_array))) {
$likes = array_unique($values_array);
$new = implode(',', $likes);
$sql = "UPDATE table SET options=" . $new . " WHERE id = '$id'";
/* seems you missed this */
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}
/* you declared sql query but not executed it */
}
}
} else {
echo "0 results";
}
$conn->close();
希望它有用:)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。