1

I want to do this query

$month = 'january'; //name of the column
$year = 2015;
$sql_query = 'SELECT "'.$month.'" FROM tbl_inpc WHERE year = "'.$year.'"';

and when I print the $sql_query it shows:

SELECT "january" FROM tbl_inp WHERE year = "2015" 

between " " double quotes and it isn't working and it should be something like this

SELECT january FROM tbl_inp WHERE year = 2015

maybe it could be something simple and stupid but my mind is blocked, some idea or am I doing something wrong? thanks.

Mi-Creativity
  • 9,554
  • 10
  • 38
  • 47
Hail Hydra
  • 472
  • 6
  • 19
  • 1
    Just use `"SELECT $month FROM tbl_inpc WHERE year = $year";` But even better would be to change your table design. It has major flaws. You should not need to patch together your query – juergen d Dec 16 '15 at 19:36
  • Just delete the double quotes inside the single quotes. `'SELECT "'` to this `'SELECT '`. – frz3993 Dec 16 '15 at 19:40
  • I deleted and it is not working yet (Unknown column '$month' in 'field list') – Hail Hydra Dec 16 '15 at 19:42
  • Like juergen d said: `"SELECT $month FROM tbl_inpc WHERE year = $year";` Notice the double quotes. If you use single quotes here, then `$month` does not get replaced by its value. – developerwjk Dec 16 '15 at 19:45
  • use juergen d or go like 'Select '.$month.' FROM tbl_inp WHERE year = "'.$year.'", you don't need the double quotes in your query where you are trying to input the month – Kebab Programmer Dec 16 '15 at 19:48
  • I did what @juergend and you said but it is displaying this message -> Unknown column '$month' in 'field list' – Hail Hydra Dec 16 '15 at 19:49
  • Type in the query you are using in these comments – Kebab Programmer Dec 16 '15 at 19:50
  • Oh, really. So the `$sql_query` looks like this `$sql_query = 'SELECT '.$month.' FROM tbl_inpc WHERE year = '.$year;` or `$sql_query = "SELECT $month FROM tbl_inpc WHERE year = $year";`? And not like this `$sql_query = 'SELECT $month FROM tbl_inpc WHERE year = $year';` ? – frz3993 Dec 16 '15 at 19:51

2 Answers2

0

In php you can use your variable inside of double quotes without exiting your string and then just escape your quotes with a backslash:

$sql_query = "SELECT \"$month\" FROM tbl_inpc WHERE year = $year";

If you want to be extra cautious you can also put your variables inside of curly brackets to make sure it is being evaluated correctly, this is particularly useful if you have an array variable with in which you are using a key:

$sql_query = "SELECT \"{$month[3]}\" FROM tbl_inpc WHERE year = {$year}";

If you want to keep what you originally had, all you need to do is remove the double quotes around the year:

$sql_query = 'SELECT "'.$month.'" FROM tbl_inpc WHERE year = '.$year;

Late Note:

OP wanted quotes removed from both $year and $month my examples demonstrate how to remove them from around $year specifically. Same approach can be taken with $month, but the example show how to to keep a variable in quotes and out of quotes in each case

Seth McClaine
  • 9,142
  • 6
  • 38
  • 64
0

Use either:

$sql_query = 'SELECT '.$month.' FROM tbl_inpc WHERE year = '.$year;

with no double quotes around the parts of the query where you substitute variables, or:

$sql_query = "SELECT $month FROM tbl_inpc WHERE year = $year";

Note that this uses double quotes around the whole string, because variables don't get substituted inside single quotes.

For more details, see

When to use single quotes, double quotes, and backticks in MySQL

What is the difference between single-quoted and double-quoted strings in PHP?

Community
  • 1
  • 1
Barmar
  • 741,623
  • 53
  • 500
  • 612