MySQL查询解析错误

时间:2015-04-29 07:47:46

标签: php 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']')");

给出以下错误:

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

我知道问题在于子查询。有什么建议吗?

4 个答案:

答案 0 :(得分:1)

试试这个你必须设置花括号{$_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']}')");

答案 1 :(得分:1)

您可以使用以下答案,因为问题在于$ _SESSION变量

$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']."')");

答案 2 :(得分:0)

错误在于:

$_SESSION['username']

两个单引号与外面的另外两个冲突。你可以像@Osama Jetawe那样做,或者你可以将这个值分配给这样的变量:

$username = $_SESSION['username'];

然后在查询中使用'$username'

答案 3 :(得分:0)

试试这个

  $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']."')");