Database returning no value

Asked

Viewed 82 times

0

I have the following code:

 $sql="SELECT * FROM imoveis WHERE 
          (suite LIKE '%".$suites."%' 
           OR banheiro LIKE '%".$banheiro."%'
           OR quarto LIKE '%".$quarto."%' 
           OR garagem LIKE '%".$garagens."%')

           AND (motivo LIKE '%".$tipo1."%'  
           OR tipo LIKE '%".$tipo."%')
           AND cidade LIKE '%".$cidadd."%' 
           AND estado LIKE '%".$local."%' 
           AND preco >= '$valor' 
           AND tamanho <= '$tamanho' ";

        $anunciarRadio=mysqli_query($conexao,$sql);

What I I need you to return to the houses in the same city as my variable with determined with the amount values of garage, suites, bathrooms, approximate rooms, with variable lower price value $value, and larger than variable size $size.

What am I doing wrong??

By putting the values exactly equal to those recorded in the database it does not return me anything... sera is a syntax or logic error ?

When I put everything to him, he returns but everyone comes. In other words, it takes a fine comb, but not so much, I need to prioritize some fields as bedrooms, bathrooms, garage, suite and the rest prioritize as being exact...

2 answers

1


This here

       AND preco >= '$valor' 
       AND tamanho <= '$tamanho' ";

wouldn’t be like this?

       AND preco >= '" .$valor. "' 
       AND tamanho <= '" .$tamanho. "'";

By correcting your select: On the between: You can see better here

     $sql="SELECT * FROM imoveis WHERE 
      (suite BETWEEN ".($suites - 2)." AND ".($suites + 2)."
       OR banheiro BETWEEN ".($banheiro - 2)." AND ".($banheiro + 2)."
       OR quarto BETWEEN ".($quarto - 2)." AND ".($quarto + 2)." 
       OR garagem BETWEEN ".($garagens - 2)." AND ".($garagens + 2).")

       AND (motivo LIKE '%".$tipo1."%'  
       OR tipo LIKE '%".$tipo."%')
       AND cidade LIKE '%".$cidadd."%' 
       AND estado LIKE '%".$local."%' 
       AND preco >= '".$valor."' 
       AND tamanho <= '".$tamanho."' ";

From what I understand you want the numbers of garage, suites, bathrooms, rooms to be close to what the user has typed so this serves to do this:

// supon que suites sejam 4
suite BETWEEN ".($suites - 2)." AND ".($suites + 2)."
// Nessa parte abaixo eu diminuo 2 para que ele procure apartir de 2  
".($suites - 2)." 
// ate 6. aqui eu aumento 2 para o valor ser 6
".($suites + 2)."

Summarizing the code will return if the property number is between 2 and 6. I hope I’ve made myself clear and helped you now.

ps: not php manjo so I’m not sure that .($suites - 2). is correct operation, but I think yes by other languages are like a ,hug. : D

  • I made that correction and still nothing comes. , I believe it is related to AND and OR @.@ was studying the use of them but I did not understand correctly, how to use in the way described above with quotation marks can leave some fields as not so necessary and others as being obligatory igua, however may have grammatical errors

  • Well I’ll try to pass it to you quickly, AND you use when you want the returned value to be what the comparison variable contains if it does not contain it will not return, if in your code if the 'immovable' you are looking for in select has all variables equal to the comparison or in the case of 'AND var like %something%' the value 'var' is contained inside 'something' it will return oimovel, now if any variable of the comparison AND returns false, ie does not contain, your select will not return anything.

  • Note that I did not mention the OR because the OR is simpler if it finds the value it returns but if it does not find the corresponding value it will not return if it is comparing alone. Example "select * from immovables Where 1 = 0 OR name like '%nameImovel%' " Here Where 1=0 is so forever to be 0(false) or if OR is 1 it returns and is 0 it returns nothing. In the case of select if only the OR is true it will not return and if only the OR is false and the AND are true it will return

  • Summarizing AND will only return when all conditions are True. OR will return when any of the conditions are met.

  • 1

    I updated my answer see if it can now be of help.

  • It worked thank you.. that’s what was in doubt.. Perfect congratulations thank you.

  • I’m happy to help :D

Show 2 more comments

0

this variable is the same?

AND cidade LIKE '%".$cidadd."%'

and the example of friend @Will can be like this too.

AND preco >= '{$valor}' 
AND tamanho <= '{$tamanho}'";
  • opa, the variable is like that, I believe it is related to AND and OR @.@ I was studying the use of them but I did not understand correctly, how to use in the way described above with quotation marks can leave some fields as not so necessary and others as being obligatory igua, however there may be grammatical errors.

Browser other questions tagged

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