PHP mysql search multiple tables using a keyword

前端 未结 4 672
时光说笑
时光说笑 2020-11-28 04:01

I have three tables in my database which are:

messages
topics
comments

Each of these tables has two fields called \'content\' and \'title\'

相关标签:
4条回答
  • 2020-11-28 04:49

    Html Search form:

    <div class="header_top_right">
        <form action="search.php" method="GET" class="search_form">
            <input type="text" placeholder="Text to Search.." name="search">
            <input type="submit" class="btn btn-default" value="">
        </form>
    </div>
    

    Search.php:

    <?php
        if (isset($_GET['search']) || !empty($_GET['search'])) {
            $search = mysqli_real_escape_string($db->link, $fm->validation($_GET['search']));
        }
        else{
            header("Location:404.php");
        }
    ?>
    <?php
        $query = "SELECT * FROM news_post WHERE title LIKE '%$search%' OR body LIKE '%$search%' OR tags LIKE '%search%'";
        $post = $db->select($query);
        if ($post) {
            while ($result = $post->fetch_assoc()) {
                echo"Database data like, $result['title']";
            }
        }
        else{
            echo "result Not found";
        }
    

    include database.php in search.php

    class Database{
        public function select($query){
            $result = $this->link->query($query) or die($this->link->error.__LINE__);
            if($result->num_rows > 0){
                return $result;
            }
            else {
                return false;
            }
        }
    }
    $db = new Database();
    
    0 讨论(0)
  • 2020-11-28 04:55

    Two search in other tables you use:

    SELECT `categories`.`title`, `posts`.`title` WHERE `categories`.`title` LIKE {$a} OR `posts`.`title` LIKE {$a}
    

    The CATEGORIES and POSTS are tables of your database.

    0 讨论(0)
  • 2020-11-28 05:01
    $query = "(SELECT content, title, 'msg' as type FROM messages WHERE content LIKE '%" . 
               $keyword . "%' OR title LIKE '%" . $keyword ."%') 
               UNION
               (SELECT content, title, 'topic' as type FROM topics WHERE content LIKE '%" . 
               $keyword . "%' OR title LIKE '%" . $keyword ."%') 
               UNION
               (SELECT content, title, 'comment' as type FROM comments WHERE content LIKE '%" . 
               $keyword . "%' OR title LIKE '%" . $keyword ."%')";
    
    mysql_query($query);
    

    So, you are getting result from all of the three tables, and you can identify which row came from which table by looking at its type value.

    0 讨论(0)
  • 2020-11-28 05:04

    What you are probably looking for is the UNION command:

    SELECT id, 'messages' as 'table' FROM messages 
      WHERE content LIKE '%keyword%' 
        OR title LIKE '%keyword%'
    UNION
    SELECT id, 'topics' as 'table' FROM topics
      WHERE content LIKE '%keyword%' 
        OR title LIKE '%keyword%'
    UNION
    SELECT id, 'comments' as 'table' FROM comments
      WHERE content LIKE '%keyword%' 
        OR title LIKE '%keyword%'
    
    0 讨论(0)
提交回复
热议问题