0

In my php I make this query

$sql = "SELECT * FROM session WHERE sessionid = '$_SESSION["id"]';";

which results in an error

Parse error: syntax error, unexpected '"', expecting '-' or identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in /opt/lampp/htdocs/Chore-Champs/index.php on line 6

Obviously there is something wrong with how I'm nesting the quotes, so I've tried different ways, including

$sql = "SELECT * FROM session WHERE sessionid = " . $_SESSION['id'] . ";";

this still results in the same error.

Normally the first method would work with normal variables such as $username, but I guess that session variables are handled differently. What's the correct way to write this query?

Andrew
  • 357
  • 5
  • 16
  • 1
    You need to enclose `$_SESSION["id"]` in `{}` to protect the `"`s around `id` i.e. $sql = "SELECT * FROM session WHERE sessionid = '{$_SESSION["id"]}';"; – Nick Jan 19 '19 at 05:21
  • 2
    I would highly recommend using prepared statements see here: https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php?rq=1 – umarsa Jan 19 '19 at 05:29

2 Answers2

0

Try

$sql = "SELECT * FROM session WHERE sessionid = '" . $_SESSION['id'] . "';";

A basic string concatenation in php

Andrew
  • 357
  • 5
  • 16
Sanjit Bhardwaj
  • 893
  • 7
  • 13
0

try this:

$sql = "SELECT * FROM session WHERE sessionid = '". $show. "'";
PHP Geek
  • 3,949
  • 1
  • 16
  • 32