-2

I am trying to construct a simple SELECT query and am not sure of the correct syntax using single and double quotes

$check = mysql_query("SELECT * FROM analysed WHERE team = ".'$team');
kerry
  • 671
  • 1
  • 5
  • 9
  • 1
    Possible duplicate of [What is the difference between single and double quotes in SQL?](http://stackoverflow.com/questions/1992314/what-is-the-difference-between-single-and-double-quotes-in-sql) – Nico Sep 03 '16 at 09:53
  • For string limitation in the query itself always use a single quote. To limit the query string in PHP use a double quote. Example: `"some string that uses an 'inner string' "` – juergen d Sep 03 '16 at 09:53
  • try this $check = mysql_query("SELECT * FROM analysed WHERE team = ' ".$team." ' "); – Numan Ali Sep 03 '16 at 09:55
  • `mysql_*` functions are deprecated as of PHP 5.5 and are removed altogether in PHP 7.0. Use [`mysqli`](http://php.net/manual/en/book.mysqli.php) or [`pdo`](http://php.net/manual/en/book.pdo.php) instead. [And this is why you shouldn't use mysql_* functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). – Rajdeep Paul Sep 03 '16 at 10:00

1 Answers1

2

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.

SierraOscar
  • 17,507
  • 6
  • 40
  • 68
  • Excellent - thanks so much! – kerry Sep 03 '16 at 10:18
  • Actually just checked further all the above seem to return a result that a record exists even if it does not. It works ok with LIKE but not = do you have any idea why? thanks again – kerry Sep 03 '16 at 10:29
  • `mysql_query` returns true if the query was sucessful - nothing to do with if there was actually a record. To check if there is a record you would have to use some other method - but it's technically a different question altogether and out of scope for this post. FYI, you should be using `mysqli_query` as `mysql_query` is depreciated and isn't secure. – SierraOscar Sep 03 '16 at 10:36