kerry kerry - 3 months ago 9
PHP Question

Correct syntax for a php SELECT query with a variable

I am trying to construct a simple SELECT query and just cannot get the right syntax !

$check = mysql_query("SELECT * FROM analysed WHERE team = ".'$team');

Answer

Single quotes are for literal strings, and in the code above will produce the string $team (literally) - but you also need those single quotes in your query if $team is a string due to MySQL syntax:

$check = mysql_query("SELECT * FROM analysed WHERE team = '".$team. "'");
$check = mysql_query("SELECT * FROM analysed WHERE team = '{$team}'");
$check = mysql_query("SELECT * FROM analysed WHERE team = '$team'");

All of the above should work.

Double quotes (or 'magic' quotes) allow variables to be used as part of the string.