php中的sql无法正常编码?

时间:2014-09-19 14:47:18

标签: php mysql sql wordpress

当我执行phpmyadmin时我的SQL代码工作,但是当我在我的php脚本中运行它时它不返回任何我认为它可能是编码(是utf8)所以我将其更改为ansi但没有区别。你们其中一个人可能会看到它有什么问题吗? (这是一个从Wordpress帖子中删除图像的脚本)

代码:

$query = "SELECT * FROM `wp_posts` where post_content like'%<img%\>'" or die("Error in the consult.." . mysqli_error($link));//THIS IS THE QUERY THAT DOES NOT WORK
$result = $link->query($query);

/*function filter($toFilter)
{
    $pattern = "/<img.*?>/";
    $filtered = "";
    if(preg_match($pattern,$toFilter))
    {
       $filtered = preg_replace($pattern,'',$toFilter);
    }
    return (string)$filtered;

}
*/
    while($row = mysqli_fetch_array($result)) {
        $id = $row['ID'];
        $toFilter = $row['post_content'];
        $toFilter = (string)$toFilter;

        $pattern = "/<img.*?>/";
        $filtered = "";
        if(preg_match($pattern,$toFilter))
        {
            $filtered = preg_replace($pattern,'',$toFilter);
        }

        $filtered = (string)$filtered;

        $link->query("update wp_posts set post_content = $filtered  where ID = $id;");

        echo 'this works';
    }

1 个答案:

答案 0 :(得分:0)

  1. 你必须在'%<img%\>'中逃避这个斜线(除非你的意思是逃避>?)
  2. 图片没有结束标记(http://www.w3.org/TR/html401/struct/objects.html#edef-IMG),因此您也可以查找起始标记
  3. 留在utf8
  4. $pattern = "/<img.*?>/";可能只是$pattern = "/<img.*>/";*表示0或更多,而?意味着可选,所以它非常冗余