如何在php中获取选定的数据库值作为会话变量? [英] How to get a selected database value as a session variable in php?

查看:37
本文介绍了如何在php中获取选定的数据库值作为会话变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取 users 表中的 id 作为名为 $_SESSION['id'] 的 PHP 会话变量.但是当我打印会话变量时,我得到了 Undefined index 作为输出.

I want to get the id in users table as a PHP session variable called $_SESSION['id']. But when I am printing the session variable, I have got Undefined index as the output.

这是我的 PHP 代码.

Here is my PHP code.

$jsqla = mysql_query("select id, user_name, user_type from users where pw='$pass' and email='$email'") or die("Website under maintenance.");
$jfeta = mysql_fetch_assoc($jsqla);

$id = $jfeta['id'];
$_SESSION['id'] = $id;

推荐答案

您是否使用 session_start 启动会话?

Did you used session_start to start a session?

<?php
session_start();
?>

之后

$id = $jfeta['id'];

应该可以:

<?php
    $jsqla = mysql_query("select id, user_name, user_type from users where pw='".$pass."' and email='".$email."'") or die("Website under maintenance.");
    $jfeta = mysql_fetch_assoc($jsqla);

    session_start();
    $id = $jfeta['id'];
    $_SESSION['id'] = $id;
?>

<小时>

您的选择也可能是问题所在:


your Selection could be the Problem too:

$jsqla = mysql_query("select id, user_name, user_type from users where pw='$pass' and email='$email'") or die("Website under maintenance.");

可以更好地工作:

$jsqla = mysql_query("select id, user_name, user_type from users where pw='".$pass."' and email='".$email."'") or die("Website under maintenance.");

<小时>

未定义索引"消息意味着您使用的一个值未设置.您可以通过示例防止这种情况:


The message "Undefined index" means that the one value you use isn't set. you can prevent this by example:

if(isset($jfeta['id']))
{
    //do thing
}

这篇关于如何在php中获取选定的数据库值作为会话变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆