Jump to content

unexpected T_ENCAPSED_AND_WHITESPACE


JKershner

Recommended Posts

I'm new to this area of php but I'm getting the error on line 70 which is:

 

$qry="SELECT * FROM troop WHERE login='<?php echo $_SESSION['SESS_LOGIN_NAME'] ?>'";

 

The user has already logged on and trying to use the session login ID which matches the field in MySQL, this will work if I replace the php code with a specific value but not the stored session.

 

Can anyone help?

Link to comment
Share on other sites

Yes because that code is completely invalid.  You are already in a php block ... you just need to use the proper interpolation.

 

$qry="SELECT * FROM troop WHERE login='{$_SESSION['SESS_LOGIN_NAME']}'";

 

I just realized that also but I'm now getting:

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\xampp\htdocs\troop.php on line 38

Link to comment
Share on other sites

Yes because that code is completely invalid.  You are already in a php block ... you just need to use the proper interpolation.

 

$qry="SELECT * FROM troop WHERE login='{$_SESSION['SESS_LOGIN_NAME']}'";

 

I just realized that also but I'm now getting:

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\xampp\htdocs\troop.php on line 38, I'm sorry, I just did a include() as the origional script was getting messy but line 38 is the same line as stated about as 70

 

Sorry for the confusion

Link to comment
Share on other sites

Syntax is a very specific thing.  You have to look carefully at things.  This is what you have at line 38:

 

$qry="SELECT * FROM troop WHERE login=('$_SESSION['SESS_LOGIN_NAME'])'";

 

That is not what I showed you. 

 

All you are trying to do is get php to interpolate (add the variables at runtime) into the string you are assigning to $qry. 

 

Usually you could just use the variable name:  "Hello $name". 

 

Because this is an array element where you want to use the proper array key literal, that will not parse. 

 

If you tried:  $msg = "Hello $_SESSION['SESS_LOGIN_NAME']";  you'll see that you get a parsing error from php.  In order to allow the interpolation of the variable, you need to but a php block around it, so that it gets resolved and will work properly.  This is just a quirk of the way php interpolation works.  Some people try and get around this using concatenation, although personally i think the code is typically simpler and easier to read if you use interpolation.

 

The php block, you should probably already know is "{ // php block here}".  This is used in control structures all the time (if, then, else, while, switch, etc.).  You did not use the block character, you used parens which makes php think you're trying to execute a function.  This is why the parser blows up.

 

Of course you weren't even that careful with the parens, because they need to go around the variable itself and have nothing else, and you have a paren that is outside the single quote literal you need for the sql statement. 

 

What the code needs to be is:

 

$qry="SELECT * FROM troop WHERE login='{$_SESSION['SESS_LOGIN_NAME']}'";

 

I don't know any other way to say it other than, you have to be detail oriented or you're going to be flailing like this often.  You have to understand the difference within php of using single quotes, double quotes, parens and braces, not to mention backtics, semicolons, and a slew of other special characters. 

Link to comment
Share on other sites

EDIT: Must have posting while gizmola was. Definitely pay attention to what he said ^^^

 

You've used parentheses instead of curly braces to enclose the $_SESSION var, and you've misplaced one of the quotes. Compare what's in your reply above (#4), to what's on line 38 in the file you attached.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.