我有一个字段(描述)在MySQL数据库(poi)。我希望使用php和strip_tags从数据库中的所有记录中删除HTML。然后,我想将结果更新为数据库中相同的Description字段。
我对获取字符串和剥离HTML没有问题,但我似乎无法弄清楚如何用结果更新数据库。
// check connection
if(mysqli_connect_errno()) {
echo "Connection Failed: " . mysqli_connect_errno();
exit();
}
$sql_article = "SELECT Description FROM poi";
$result = $mysqli->query($sql_article);
// Iterates through the MySQL results
while ($row = $result->fetch_array())
{
$Description_no_html = strip_tags($row['Description']);
printf("%s<br />'n", $Description_no_html);
}
理想情况下,每一行都有一个唯一的id列,您可以使用该列指定要使用准备好的语句更新哪一行:
$sql_article = "SELECT id, Description FROM poi";
$result = $mysqli->query($sql_article);
$stmt = $mysqli->prepare("UPDATE poi SET Description = ? WHERE id = ?");
// Iterates through the MySQL results
while ($row = $result->fetch_array())
{
$Description_no_html = strip_tags($row['Description']);
printf("%s<br />'n", $Description_no_html);
$stmt->bind_param("si",$Description_no_html,$row['id']);
$stmt->execute();
}
如果没有唯一的id列,则使用以下语句代替
$stmt = $mysqli->prepare("UPDATE poi SET Description = ? WHERE Description = ?");
和
$stmt->bind_param("ss",$Description_no_html,$row['Description']);
可选:直接在mysql中剥离标签
您可以创建一个自定义的mysql函数来剥离标签(https://stackoverflow.com/a/13346684/3574819),并使用以下查询
UPDATE poi SET Description = strip_tags(Description)
免责声明:我不确定上面引用的mysql strip_tags
有多好,所以你的里程可能会根据你的内容而有所不同。