MySQL查询解析错误[关闭]

$week_leave = mysql_query("select count(id),leave.id from `leave`, `users` where leave.backgroundColor = 'Green' and leave.id=(select U_id from `users` where users.username='$_SESSION['username']')");

Gives the following error:

Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) 

I know the problem lies in the sub query. Any suggestions ?

try this you have to set the data vetween curly braces {$_SESSION['username']}

$week_leave = mysql_query("select count(id),leave.id from `leave`, `users` where leave.backgroundColor = 'Green' and leave.id=(select U_id from `users` where users.username='{$_SESSION['username']}')");

The error is here:

$_SESSION['username']

the two single quotes conflict with the other two outside. You can do as @Osama Jetawe suggets or you can just assign this value to a variable like this:

$username = $_SESSION['username'];

and then use '$username' in your query

You can use this below answer as problem lies at $_SESSION variable

$week_leave = mysql_query("select count(id),leave.id from `leave`, `users` where leave.backgroundColor = 'Green' and leave.id=(select U_id from `users` where users.username='".$_SESSION['username']."')");

try this

  $week_leave = mysql_query("select count(id),leave.id from `leave`, `users` where leave.backgroundColor = 'Green' and leave.id=(select U_id from `users` where users.username='".$_SESSION['username']."')");