-2

line 21:-

$sql="SELECT DISTINCT sem from $_SESSION['ye']";

and I get an error

" Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in C:\xampp\htdocs\try1\Model\attendanceModel.php on line 21"

FelixSFD
  • 6,052
  • 10
  • 43
  • 117
Rishabh Anand
  • 167
  • 3
  • 12
  • While it's ugly and unsafe to build SQL like this, PHP does allow putting in a "complex variable expansions" in a string interpolation like so: `$str = "blah blah {$_SESSION['ye']}"`. Note the wrapping braces and, that unlike other languages which allow any expression, the construct is limited to variable expansions. – user2864740 Aug 02 '16 at 04:25

4 Answers4

3

First assign table name to variable

$tableName = $_SESSION['ye'];

Then use the variable name in query

$sql = "SELECT DISTINCT sem from `$tableName`";

Or simply use

$sql = "SELECT DISTINCT sem from `". $_SESSION['ye']."`";
Deepak Adhikari
  • 419
  • 2
  • 4
3

Use something like this

$sql="SELECT DISTINCT sem from ".$_SESSION['ye'];
Rakesh Kumar
  • 4,319
  • 2
  • 17
  • 30
1

When interpolating array items into a string you need to use the complex interpolation syntax (wrap it in {}):

$sql = "SELECT DISTINCT `sem` from `{$_SESSION['ye']}`";

It is generally simpler to just save the value to an auxiliary variable. It can make your code easier to read:

$ye  = $_SESSION['ye'];
$sql = "SELECT DISTINCT `sem` from `$ye`";
Sverri M. Olsen
  • 13,055
  • 3
  • 36
  • 52
-1

You have keep the script like this.

$sql="SELECT DISTINCT sem from ".$_SESSION['ye'];
Sunil kumar
  • 761
  • 5
  • 15
  • this is character for character identical to the upvoted answer from 19 minutes ago. – OGHaza Aug 02 '16 at 04:37
  • But, it does not mean to stop answering questions.. – Sunil kumar Aug 02 '16 at 04:39
  • Welcome to SO. Though we thank you for your answer, it would be better if it provided additional value on top of the other answers. In this case, your answer does not provide additional value, since [Rakesh Kumar](http://stackoverflow.com/users/6540780/rakesh-kumar) already posted that solution. If a previous answer was helpful to you, you should [vote it up](http://stackoverflow.com/help/privileges/vote-up). – OGHaza Aug 02 '16 at 04:44