IF comparing with GET does not work

Asked

Viewed 90 times

0

Hello. I’m trying to make a comparison in PHP and IF doesn’t work for nothing. I’ve tried several ways and it didn’t work.

The code is:

        if(($_GET['categoria']) == 0) {
    $mysql = new conexao();
    echo 'Erro';
    }else{
    $mysql = new conexao();
    $result = $mysql->sql_query("select * from TB_GALERIA where CATEGORIA=".$_GET['categoria']." ");
    while ($row = mysql_fetch_assoc($result)) {
            echo '<div class="item">';
            echo '<a class="item-hover" href="../../../uploads/midia/15989/maior_maior_DSC_0108.jpg">';
            echo '<img src="images/'.$row["IMAGEM"].'" alt="imagem galeria" class="img-responsive lazyOwl" data-src="/images/';
            echo $row["IMAGEM"];
            echo '">';
            echo '</a>';
            echo '</div>';

        }
    }

What is wrong with the if? Thank you!

  • Is there a mistake?

  • What tests were performed? Verified what is the value of $_GET What’s coming up? Put that in the question too.

  • It does not return an error message, it is just as if $_GET does not return anything. But for example, if I open an existing gallery (with her id), it opens normally. It is as if the searched category was not found, instead of doing the ordered action in IF. $_GET is pulling the values normally, since when accessing the existing categories they normally open.

3 answers

2


First, before accessing a value within $_GET it is best to check if the value exists using isset.

Second, it’s better compare with three equal signs "===", so you compare the value and the type, because "false == 0" is true, but "false === 0" is false:

if( isset($_GET['categoria']) )
{
    if( $_GET['categoria'] === 0 )
    {
        // faz tudo que tem que fazer...
    }
}

To see what’s inside the $_GET, I suggest doing the following:

echo "<pre>";
var_dump($_GET);
echo "</pre>";

die(); // pra não continuar o script, e você focar no $_GET

If the comparison is not resulting in something expected, then the value of $_GET has not been filled in as it should.

1

If you call only the script, without passing any value in the parameters it will always be 0, in this case you should first check if it was passed using

    if ( ! isset( $_GET[ 'categoria' ] ) {
            echo 'Faltou a categoria';
            exit;
    }

    if ( $_GET[ 'categoria' ] == 0 ) {
            // ...
    } else {
            // ...
    }

I think this is it!

0

If you want to keep the same way you are making the mistake is in the relatives you put in the if. Below is the correction.

if($_GET['categoria'] == 0) {
$mysql = new conexao();
echo 'Erro';
}else{
$mysql = new conexao();
$result = $mysql->sql_query("select * from TB_GALERIA where CATEGORIA=".$_GET['categoria']." ");
while ($row = mysql_fetch_assoc($result)) {
        echo '<div class="item">';
        echo '<a class="item-hover" href="../../../uploads/midia/15989/maior_maior_DSC_0108.jpg">';
        echo '<img src="images/'.$row["IMAGEM"].'" alt="imagem galeria" class="img-responsive lazyOwl" data-src="/images/';
        echo $row["IMAGEM"];
        echo '">';
        echo '</a>';
        echo '</div>';

    }
}

Browser other questions tagged

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