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

时间:2018-08-06 12:21:22

标签: php mysql mysqli

我想从数据库中删除一个以<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);

?>

能帮我吗?谢谢。

1 个答案:

答案 0 :(得分:-1)

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);

?>