php search with UNION - mysql_num_rows error

Asked

Viewed 44 times

0

I am making a simple search system that brings only the amounts of results from the specified tables The problem is that it is causing an error due to the different tables and their lines. Check out my script.

$q = $_GET['busca'];

$query= '
        SELECT * FROM noticia WHERE noticia_title LIKE "%'.$q.'% or noticia_content LIKE "%'.$q.'%"
    UNION
        SELECT * FROM eventos WHERE evento_nome LIKE "%'.$q.'% or evento_content LIKE "%'.$q.'%"
        UNION 
    SELECT * FROM albuns WHERE album_name LIKE "%'.$q.'%" or album_descricao LIKE "%'.$q.'%"  
    ';

    $result = mysql_query($query);
    $count = mysql_num_rows($result);

    if ($count == 0) {
        echo "Nenhum resultado!";
} else {
    if ($count == 1) {
        echo "1 resultado encontrado!";
}
        if ($count > 1) {
        echo "$count resultados encontrados!";
    }
        while ($dados = mysql_fetch_array($query)) {
        echo "";
    }
    }              

The mistake being made is: Warning: mysql_num_rows(): supplied argument is not a Valid Mysql result Resource in

Can someone help me?

  • You are forgetting to close quotes in two points... 1) SELECT * FROM noticia WHERE noticia_title LIKE "%'. $q. '%" 2) SELECT * FROM events WHERE evento_name LIKE "%'. $q.'%"

  • @Fernandosouza I made the correction, I had not even noticed. But the error persists. Warning: mysql_num_rows(): supplied argument is not a Valid Mysql result Resource

  • tries to do so: mysql_query($query) or die(mysql_error()); and sees what appears

  • Gave the following error: The used SELECT statements have a Different number of Columns

  • The tables [news , events and albums] have the amount of different columns.. if you can edit your question and put the structure of these tables, it will help a lot.

  • Hello, I put, look !

  • So... the mistake you’re having is why you can’t select * from these tables using Union why the number of columns is different..

  • What I can do to solve this selection and results. It has how to help?

Show 3 more comments

1 answer

3


As discussed in the comments:

1) Double quotes were missing in the first two selects

SELECT * FROM noticia WHERE noticia_title LIKE "%'.$q.'%" 
SELECT * FROM eventos WHERE evento_nome LIKE "%'.$q.'%"

2) After adding mysql_query($query) or die(mysql_error()); the following error has been identified:

The used SELECT statements have a Different number of Columns

3) To solve this problem you must make a SELECT with the same number of columns. Example:

$query= '
        SELECT noticia_id FROM noticia WHERE noticia_title LIKE "%'.$q.'%" or noticia_content LIKE "%'.$q.'%"
    UNION
        SELECT evento_id FROM eventos WHERE evento_nome LIKE "%'.$q.'%" or evento_content LIKE "%'.$q.'%"
    UNION 
        SELECT album_id FROM albuns WHERE album_name LIKE "%'.$q.'%" or album_descricao LIKE "%'.$q.'%"  
    ';
  • Hello mate! It worked now! Ball show! Could you explain to me why it was necessary to put the first column of the table in each SELECT?

  • When you use the UNION operator in a SELECT you have to pay attention to some details like: 1) The number of columns should be equal.. 2) The data type should be similar... 3) The columns should be in the same order... See this one Link where he talks a little more about it. Ah! and if possible accept my answer as the correct one.

  • Ah got it! Thank you so much for the support!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.