donglin9717
2016-09-22 06:01 阅读 82
已采纳

mysql查询没有显示结果

can anyone help me to solve this mysql query problem

$rela=mysql_query("SELECT * 
      FROM  pvl_posts 
      WHERE (title LIKE '%$title%' OR post LIKE '%$title%'') 
      OR (title LIKE '%$post%' OR post LIKE '%$post%'') 
      AND id != '$id'");

I want use it as related articles

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

4条回答 默认 最新

  • 已采纳
    duanqiang2617 duanqiang2617 2016-09-22 06:11

    You have some wrong quotes and use concat for the like string

    "SELECT * 
      FROM  pvl_posts 
      WHERE (title LIKE concat('%', '$title','%') OR post LIKE concat('%','$title' ,'%') )
      OR (title LIKE concat('%' , '$post', '%')   OR post LIKE concat('%','$post','%') )
      AND id != '$id'"
    
    点赞 评论 复制链接分享
  • douyuan9512 douyuan9512 2016-09-22 06:05

    Please try the following:

    $rela=mysql_query("SELECT * FROM  pvl_posts WHERE (title LIKE '%".$title."%' OR post LIKE '%".$title."%') OR (title LIKE '%".$post."%' OR post LIKE '".$post."') AND id != '".$id."'");
    
    点赞 评论 复制链接分享
  • duancaiyi7567 duancaiyi7567 2016-09-22 06:05

    try this,there is two ' ,remove it.

    $rela=mysql_query("SELECT * FROM  pvl_posts WHERE (title LIKE '%$title%' OR post LIKE '%$title%') OR (title LIKE '%$post%' OR post LIKE '%$post%') AND id != '$id'");
    
    点赞 评论 复制链接分享
  • duancan5327 duancan5327 2016-09-22 06:28

    Kindly show your mysql connection string. here is an example:

    <?php
    $mysqli = new mysqli("localhost", "root", "", "database_name");
    $query = "SELECT * FROM  pvl_posts WHERE (title LIKE '%$title%' OR post LIKE '%$title%') OR (title LIKE '%$post%' OR post LIKE '%$post%') AND id != '$id'";
    $rows = $mysqli->query($query);
    if ($rows->num_rows > 0) {
        while ($row = $rows->fetch_assoc()) {
            // do something with row variable here.
            var_dump($row);
        }
    }
    ?>
    

    Try it out.

    点赞 评论 复制链接分享

相关推荐