简体   繁体   中英

How to display the datas from two tables using search query?

I need to display datas from particular based on the results. I have two tables movies_info => has all movies info & tv_shows_info => has TV shows info. I have search box if i enter the search it should search from both the table and display the datas but the condition in the results if it has movie_id, it should show movie details and if the result has tv_show_id it should show tv-show details.

as of now i have used basic search to fetch datas from one table and i have tried of using two but its not working.

<?php
// sql query for retrieving data from database
        $sql_query = "SELECT * FROM `movies_info`";
        $result = mysqli_query($connection, $sql_query);

 // SQL Query for filter
        if(isset($_POST['search_button']))
        {
            $value_to_search = $_POST['value_to_search'];

            // search in all table columns
            // using concat mysql function

            $search_query = "SELECT * FROM `movies_info` WHERE CONCAT(`movie_name`, `movie_original_name`, `release_year`, `movie_genre`, `movie_country`, `movie_stars`, `movie_director`) LIKE '%".$value_to_search."%'";

            //$search_query = "SELECT * FROM `movies_info`,`tv_shows_info` WHERE CONCAT(`movie_name`, `release_year`, `movie_genre`, `movie_country`, `tv_show_name`) LIKE '%".$value_to_search."%' GROUP BY `movie_id`";

            //$search_query = "SELECT * FROM `movies_info` UNION ALL `tv_shows_info` WHERE CONCAT(`movie_name`, `release_year`, `movie_genre`, `movie_country`, `tv_show_name`) LIKE '%".$value_to_search."%'";
            //$search_query2 = "SELECT * FROM `tv_shows_info` WHERE CONCAT(`tv_show_name`, `tv_show_start_year`, `tv_show_end_year`, `tv_show_genre`, `tv_show_country`) LIKE '%".$value_to_search."%'";
            //$search_query .= $search_query2;
            $search_result = filterTable($search_query);

        }

        else {
            $search_query = "SELECT * FROM `movies_info`";
            $search_result = filterTable($search_query);        
                //echo 'No Search Results Found';
        }

    ?>  

<!-- /w3l-medile-movies-grids -->
    <div class="general-agileits-w3l">
        <div class="w3l-medile-movies-grids">

                <!-- /movie-browse-agile -->

                      <div class="movie-browse-agile">
                         <!--/browse-agile-w3ls -->
                        <div class="browse-agile-w3ls general-w3ls">
                                <div class="tittle-head">
                                    <h4 class="latest-text">Search Results for : "<?php echo $value_to_search ?>"</h4>
                                    <div class="container">
                                        <div class="agileits-single-top">
                                            <ol class="breadcrumb">

                                              <li><a href="index.php">Home</a></li>
                                              <li class="active" style="text-transform:Capitalize;">Search Results </li>
                                            </ol>
                                        </div>
                                    </div>
                                </div>
                                     <div class="container">
                                    <div class="browse-inner">

                                        <?php

                                            echo $search_result;

                                            $rowcount = mysqli_num_rows($search_result);

                                            for($i=1;$i<=$rowcount;$i++){
                                            $row=mysqli_fetch_array($search_result);


                                        ?>

                               <div class="col-md-2 w3l-movie-gride-agile">
                                         <a href="movie.php?movie_id=<?php echo $row['movie_id']; ?>" class="hvr-shutter-out-horizontal"><img src="<?php echo $row['movie_image']; ?>" title="<?php echo $row['movie_name']; ?>" alt=" " />
                                         <div class="w3l-action-icon"><i class="fa fa-play-circle" aria-hidden="true"></i></div>
                                    </a>
                                      <div class="mid-1">
                                        <div class="w3l-movie-text">
                                            <h6><a href="movie.php?movie_id=<?php echo $row['movie_id']; ?>"><?php echo $row['movie_name']; ?></a></h6>                         
                                        </div>
                                        <div class="mid-2">

                                            <p><?php echo $row['release_year']; ?></p>
                                            <div class="block-stars">
                                                <ul class="w3l-ratings">

                                                         <li> 
                                                          <span>IMDB <i class="fa fa-star" aria-hidden="true"></i> <?php echo $row['movie_rating']; ?> </span>
                                                         </li>

                                                </ul>
                                            </div>
                                            <div class="clearfix"></div>
                                        </div>

                                    </div>
                                    <div class="ribben two">
                                        <p>NEW</p>
                                    </div>  
                                                </div> <?php } ?>
                                </div>

as of now i can get the values from one table.,

My exact need is it can be Movie or TV-Show but i should get the datas from both the table if it is a movie it should show some particular info and if thats a TV-show it should show someother info.

First, you need to use prepared statements which you can use even if you are using the LIKE function in MySQL. Then you need to add spaces between the column names to prevent the values from being blended together like "star warsstar wars". This will cause a movie like "star wars to be a result when a user searches for "ss" which would be inaccurate.

$search_result = array(); 
$search_query = "SELECT * FROM `movies_info` WHERE CONCAT(`movie_name`,' ', `movie_original_name`,' ',`release_year`,' ',`movie_genre`,' ',`movie_country`,' ',`movie_stars`,' ',`movie_director`) LIKE CONCAT('%',?,'%')";
if($stmt = $db->prepare($search_query)){
        $stmt->bind_param('ii',$_SESSION['iidn_reference'],$o['parent_ref_id']);
        if($stmt and $stmt->execute()){
            $result = $stmt->get_result();
            while($row = $result->fetch_array(MYSQLI_ASSOC)){
                $search_result[] = $row;
            }
        }
}

After that, you could combine these tables with MySQL UNION but I strongly recommend just searching both tables and aliasing the table names to match.

SELECT 'movie' as `type`, `movie_id` as `id`, `movie_name` as `name`...
SELECT 'show' as `type`, `show_id` as `id`, `show_name` as `name`...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM