dtlygweb2017 2013-07-03 11:41
浏览 48
已采纳

使用PDO的未定义变量 - 混淆

I'm just starting to get to grips with using PDO over mysql_ and I'm currently in the process of converting all of the mysql_ functions into PDO. The problem I have is that I am completely stumped with these functions. The purpose of these is to take the user_id from the $_SESSION created on login and return their data from the database using that user_id. However, I'm getting the error:

Undefined variable: user_data

each time I try to check whether something is true, for example if admin = 1. This is the user_data function:

function user_data($user_id) {
    global $con;
    $data = array();
    $user_id = (int)$user_id;

    $func_num_args = func_num_args();
    $func_get_args = func_get_args();

    if ($func_num_args > 1) {
        unset($func_get_args[0]);

        $fields = '`' . implode('`, `', $func_get_args) . '`';
        $data = "SELECT $fields FROM `users` WHERE `user_id` = $user_id";
        $result = $con->prepare($data);
        $result->execute();
        $user_data = $result->fetch();

        return $user_data;
    }
}

This piece of code should call that function and destroy the session if banned = 1:

if (logged_in() === true) {
    $session_user_id = $_SESSION['user_id'];
    $user_data = user_data($session_user_id, 'user_id', 'username', 'password', 'email', 'password_recovery', 'moderator', 'admin');
    if (user_banned($user_data['username']) === true) {
        session_destroy();
        header('Location: pts.php');
        exit();
    }
}

and this is an example of where I am trying to use the $user_data variable:

<?php if ($user_data['moderator'] == 1)

I apologise if this is difficult to understand as I am a noob when it comes to coding and I'm not particularly good at explaining things. If you need any more code please just ask, I am happy to provide it.

Thank you everyone.

展开全部

  • 写回答

2条回答 默认 最新

  • douju1852 2013-07-03 11:46
    关注

    You only define $user_data if the user is logged in. You need to make sure that $user_data exists before you try to access it.

    if (!empty($user_data) && $user_data['moderator'] == 1)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 求遗传算法GAMS代码
  • ¥15 雄安新区高光谱数据集的下载网址打不开
  • ¥66 android运行时native和graphics内存详细信息获取
  • ¥100 求一个c#通过CH341读取数据的Demo,能够读取指定地址值的功能
  • ¥15 rk3566 Android11 USB摄像头 微信
  • ¥15 torch框架下的强化学习DQN训练奖励值浮动过低,希望指导如何调整
  • ¥35 西门子博图v16安装密钥提示CryptAcquireContext MS_DEF_PROV Error of containger opening
  • ¥15 mes系统扫码追溯功能
  • ¥40 selenium访问信用中国
  • ¥20 在搭建fabric网络过程中遇到“无法使用新的生命周期”的报错
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部