【问题标题】:mySQL search not really workingmySQL 搜索并没有真正起作用
【发布时间】:2012-01-13 11:32:11
【问题描述】:

这是一个 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;

    }

    ?>

【问题讨论】:

  • 是时候学习一下 SQL 是如何工作的了,我想...
  • 你有什么问题?另外,“没有真正工作”是什么意思?
  • 不错的 SQL 注入孔。希望您喜欢有人会在您的数据库中停放的卡车。

标签: php mysql search


【解决方案1】:

除其他信息外:

我可能会错过它,但我没有看到您在 while 循环中包含任何内容。

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

【讨论】:

  • 我认为这可能是问题中的一个错字,但这是一个很好的观点。
  • @Robin Knight -- 如果这是错误,你应该接受这个答案。
【解决方案2】:

您的查询未选择post_namepost_titlepost_date。你说SELECT ID FROM wp_posts,所以它选择了ID,没有别的,按照指示。你的date() 调用有点工作,因为strtotime() 返回错误,所以它使用今天的日期作为后备。

【讨论】:

  • 如果您需要post_name,您将不得不选择它。 SELECT ID, post_name, post_title, post_date FROM wp_posts ....
【解决方案3】:

这个:

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

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

【讨论】:

  • 这绝对是对我的问题的绝妙答案
猜你喜欢
  • 1970-01-01
  • 2016-12-25
  • 1970-01-01
  • 2014-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-06
  • 1970-01-01
相关资源
最近更新 更多