根据会话选择

时间:2011-04-16 17:29:52

标签: php mysql session

如何从基于会话的用户名数据库中选择一个值?

这是我到目前为止所做的:

$query = mysql_query ("select id from CUSTOMER where username = .$_SESSION['username'] ");

3 个答案:

答案 0 :(得分:2)

如果用户名在会话cookie中,那么就像这样获取用户名

$username = $_SESSION['username'];
$escuname = mysql_real_escape_string($username);
$query = mysql_query("select id from CUSTOMER where username = '".$escuname."' LIMIT 1"); 

答案 1 :(得分:1)

$query = mysql_query("select id from CUSTOMER where username = '".$_SESSION['username']."'");

答案 2 :(得分:1)

查询中的会话变量未正确解析。您可以使用花括号语法修复它:

$query = mysql_query( "select id from CUSTOMER where username = '{$_SESSION[ "username" ]}'" );

或使用点运算符连接它:

$query = mysql_query ( "select id from CUSTOMER where username = '" . $_SESSION[ "username" ] . "'" );

您可以找到有关parsing strings in PHP manual的更多信息。