syntax error, Unexpected 'if' (T_IF)

Asked

Viewed 129 times

-3

What’s the mistake?

$sql = "SELECT item1, item2, item3 FROM tab_exemplo WHERE" 
if (isset($item3)) {echo 'quartos LIKE :A';}
if (isset($item2) || isset($item1) || isset($item4)){echo 'AND'}
if (isset($item4)){echo 'garagens LIKE :B';}
if (isset($item2) || isset($item1) || isset($item3)){echo 'AND'}
if (isset($item2)){echo 'tipoNegocio LIKE :C';}
if (isset($item3) || isset($item1) || isset($item4)){echo 'AND'}
if (isset($item1)){echo 'tipo LIKE :D';} echo '\"';

1 answer

4


Try to better organize your code that will avoid simple errors like this. Note below how easy it is to read and find problems in the code. Missing ; in the completion of several lines.

Get used to also interpreting error messages and searching in the documentation about them. In programming errors happen by the piles. If every time one shows up you have to ask someone to slow down and tiring program.

Often the error appears in one place but is in another. The compiler/interpreter cannot understand intent. If a semicolon is missing that closes the command, then the error will only appear on the next line when it finds something strange in what it still considers the middle of the command (although you programmer intended it to be a new command).

Mounting SQL expressions like this can be foolhardy in terms of security depending on where these variables come from.

In the commentary it was mentioned that it would concatenate strings. Even if the problem posed in the question was this, it would still have to change a lot. Not only would I have to concatenate the variable $sql instead of using echo, but also needs to review the organization of the text since this would not produce a correct consultation. I also find it strange how to determine what to concatenate.

  • But I would like to concatenate echo s together with SELECT strings so I could not put ; after Where

  • 1

    The ; It has nothing to do with concatenation, that’s another problem. If you want to concatenate, then concatenate, but be sure to close the command. It seems a confusing way to concatenate. Mainly using echo. If you are going to concatenate you have to use the variable, do not print the text.

  • exactly that, thank you!

Browser other questions tagged

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