PHP 中的会话变量
嘿伙计们,我正在根据设置的会话变量执行一系列 if 语句(它查看数据库中的值,然后决定是否清空会话或为其命名。但是我在会话中遇到了问题总是被命名,无论数据库中的记录是否为“0”,查询在 mysql 中运行时都可以正常工作
。
$_SESSION['MemberType'] = '';
mysql_select_db($database_choices, $choices);
$query = "Select lifemember from registrants where username = '" . $_SESSION[kt_login_user] . "'";
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
while($row = mysql_fetch_array($result))
{
if ($row['lifemember'] == '1')
{
$_SESSION['MemberType'] = 'LifeMember';
}
else if($row['lifemember'] == '0')
{
$_SESSION['MemberType'] = '';
}
}
Hey guys, Im doing a series of if statements based on a session variable that is set (which looks at a value in the DB and then decides whether to empty the session, or name it. However I'm having problems in that the session is always named, regardless of whether the record in the database is '0' or not. The query works fine when run in mysql. Here's the code:
session_start();
$_SESSION['MemberType'] = '';
mysql_select_db($database_choices, $choices);
$query = "Select lifemember from registrants where username = '" . $_SESSION[kt_login_user] . "'";
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
while($row = mysql_fetch_array($result))
{
if ($row['lifemember'] == '1')
{
$_SESSION['MemberType'] = 'LifeMember';
}
else if($row['lifemember'] == '0')
{
$_SESSION['MemberType'] = '';
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
预检查:如果您收到任何“标头已发送...”,则页面上已有一些输出,这会阻止会话设置。
否则,请尝试调试步骤,例如:
1)在 while 循环内执行
echo $row['lifemember']
,以便您知道从数据库中获取的内容的真实值。2)在else块中,将其修改为
$_SESSION['MemberType'] = 'Hello';
然后看看是否打印了Hello?Precheck: If you are getting any 'Headers already sent...' then you have some output already on the page which is preventing the setting of session.
Otherwise try debugging steps like:
1) Do an
echo $row['lifemember']
inside the while loop so that you know REAL value of what's been fetched from DB.2) In else block, change it to
$_SESSION['MemberType'] = 'Hello';
Then see, whether Hello is printed or not?