mySQL搜索没有真正工作


mySQL search not really working

这是一个mySQL查询,密码和用户名被故意弄乱了。它只返回一个结果,除了这个结果的今天的日期,它不返回任何其他信息。不管你尝试什么搜索,它都不起作用。

 <?php 
    $proto = $_GET['p'];
    $terms = $_GET['f'];
    $return;
    if($proto == 'inline'){
        echo 'checking';
    $username="*******";
    $password="*******";
    $database="*******";
    $my_text = $_GET['f'];        //what I'm searching for
    $my_category = '8';        //whatever category number it is
    mysql_connect(localhost,$username,$password) or die(mysql_error());
    mysql_select_db($database) or die(mysql_error());
    $result = mysql_query("SELECT ID FROM wp_posts WHERE post_title LIKE '%$my_text%' ");
    // select all posts that have your text
    while ($row = mysql_fetch_array($result));
    $postname = $row['post_name'];
    $posttitle = $row['post_title'];
    $postID = $row['ID']; 
    $date = date ( 'd-M-Y' , strtotime($row['post_date']) );
        $return.= '
                    <a href="http://www.robin-knight.com/' . $postname .'">'.$posttitle.' ('.$postname.')<br /><span style="font-size:10px; color:#555;">'.get_the_time("d-M-Y", $postID).' - '.get_post_meta($postID, "status", true).'</span></a>
                ';
       //while have posts
       echo $return;
    }
    ?>

除其他信息外:

我可能忽略了它,但是我没有看到你在while循环中包含任何东西。

这就是为什么它只能得到一个。你需要使用大括号{}

查询不选择post_name, post_titlepost_date。你说的是SELECT ID FROM wp_posts,所以它选择了ID,没有其他,按照指示。你的date()调用有点工作,因为strtotime()返回false,所以它使用今天的日期作为回退。

This:

 mysql_query("SELECT ID FROM wp_posts WHERE post_title LIKE '%$my_text%' ");

是非常不安全的- my_text没有转义,并且从GET参数中获取,因此它使您打开SQL注入攻击。使用mysql_real_escape_string转义,或者更好的是,在代码中使用参数化查询。