php文件删除数据库中两个字符串之间的字符串

dwbf0jvd  于 2021-06-23  发布在  Mysql
关注(0)|答案(1)|浏览(282)

我想从(wordpress帖子的)数据库中删除一个以 <noindex> 结束于 </noindex> . 这些字符串之间的字符串并不总是相同的。
我发现一个php脚本似乎很旧,不起作用,它需要一些修复(如msqli),我不知道,我只是想清除数据库中的病毒:

<?php

$co = mysql_connect("localhost", "MY_USER", "MY_PASS");
mysql_set_charset('utf8');
mysql_select_db("MY_DB", $co);

$selectQuery = "SELECT ID, post_content FROM wp_posts";

$res = mysql_query($selectQuery, $co) or exit(mysql_error());

while($row = mysql_fetch_assoc($res))
{
    $updateQuery = "UPDATE wp_posts SET post_content = '" . addslashes(preg_replace("<noindex>(.*)</noindex>", '', $row['post_content'])) . "' WHERE ID = " . $row['ID'];
    mysql_query($updateQuery) or exit(mysql_error());
}

mysql_close($co);

?>

你能帮帮我吗?谢谢您。

vfwfrxfs

vfwfrxfs1#

Try this code:-
<?php
$co = mysqli_connect("localhost", "MY_USER", "MY_PASS");
mysqli_set_charset($co,'utf8');
mysqli_select_db("MY_DB", $co);
$selectQuery = "SELECT ID, post_content FROM wp_posts";
$res = mysqli_query($co,$selectQuery) or exit(mysqli_error());
while($row = mysqli_fetch_assoc($res))
{
    $updateQuery = "UPDATE wp_posts SET post_content = '" . addslashes(preg_replace("<noindex>(.*)</noindex>", '', $row['post_content'])) . "' WHERE ID = " . $row['ID'];
    mysqli_query($co,$updateQuery) or exit(mysqli_error());
}
mysqli_close($co);

?>

相关问题