Searching and displaying results 2 tables mysqli / php

1

I wanted to search 2 tables (news and photos) and then display the results of this query below. But the fields I have in each table are different and I'm not able to work out a way to display the results because of that.

News table fields: Title, photo, description, content, date

Photo table fields: Title, photo, date, location

Ex:

  • I search the term "test" ...
  • Query in both tables and brings all the results found.
  • The search resulted in the following word:

    Note: The image layout is just an example, what I can not do is the code that generates the result. If it were the result of a table only displaying results only from it I can do but displaying results from 2 tables together and merged I could not.

        
    asked by anonymous 11.09.2018 / 20:46

    1 answer

    2

    You need to match the fields of the two tables, so the number and name of the columns returned will be the same.

    SELECT 'titulo', 'foto', 'descricao', 'conteudo', 'data', '' as 'local'
    FROM 'noticia' WHERE [sua pesquisa]
    UNION
    SELECT 'titulo', 'foto', '' as 'descricao', '' as 'conteudo', 'data', 'local'
    FROM 'fotos' WHERE [sua pesquisa];
    

    And when it is displayed you validate if the field is not empty to be displayed.

    if (isset($query['campo']) && !empty($query['campo'])) {
         echo $query['campo'];
    }
    
        
    11.09.2018 / 21:59