1

This is my query for selecting a column named 'course':

$query = "SELECT course from surprise WHERE diff_lvl = "'.$level.'" AND
          tot_days = "'.$time_day'" AND tot_time = "'.$time_tot.'" "; \\(it is basically single quote inside a double quotes)

But I am getting error as:

syntax error, unexpected ''.$level.'' (T_CONSTANT_ENCAPSED_STRING) in C:\xampp\htdocs\Test\sup_logic.php.

What should I do to fix it?

Is there any way I can write a query to get the value of course column?

ata
  • 3,398
  • 5
  • 20
  • 31
AllSmiles
  • 53
  • 2
  • 7
  • Your quotes are in the reverse. You opened a double quoted string `"SELECT..."` so move the single quote (which is part of the syntax around the `diff_lvl` value) _inside_ the double quotes. Looks like the same for `$time_tot`. --- `WHERE diff_lvl = '". $level ."' ...` – Michael Berkowski Apr 21 '18 at 18:11
  • This looks like it could have been alleviated by using prepared statements instead of concatenated variables. But see also [When to use single quotes double quotes and backticks in MySQL](https://stackoverflow.com/questions/11321491/when-to-use-single-quotes-double-quotes-and-back-ticks-in-mysql/11321508#11321508) for several examples of what a string with variables like this in SQL could be expressed as. – Michael Berkowski Apr 21 '18 at 18:12

1 Answers1

1

Try this query

$query = "SELECT course from surprise WHERE diff_lvl = '$level' AND tot_days = '$time_day' AND tot_time = '$time_tot'";

While using double quotes for query you can user single quotes too like this or if you prefer the way you used replace the below one

$query = "SELECT course from surprise WHERE diff_lvl = '".$level."' AND tot_days = '".$time_day."' AND tot_time = '".$time_tot."'";

Notice the changes in single quote and double qoute