开发者

Undefined Index [duplicate]

开发者 https://www.devze.com 2023-03-13 06:35 出处:网络
This question already has answers here: "Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset&quo
This question already has answers here: "Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP 开发者_如何学C (29 answers) Closed 6 years ago.

I'm getting an undefined index error in this code:

function get_userdata() {
                    global $db;
                    global $user;
                    global $data, $value, $line;
                    $id = $_SESSION['exp_user']['userid'];
            $row = $db->query("SELECT * FROM tbl_staff WHERE id = $id");
        $user = array();
        while ($line = $row->fetch_assoc())
             {$user[ $line['data'] ] = intval($line['value']);}
}

Error being:

Notice: Undefined index: data in /includes/functions.php on line 11

Notice: Undefined index: value in /includes/functions.php on line 11

Line 11 being {$user[ $line['data'] ] = intval($line['value']);}.

What am I doing wrong?

Thanks


I am assuming your variable $line['data'] does not return a primitive value, i.e. an int or a string.

Here is how i would build the array.

while ($line = $row->fetch_assoc()){    
    $hc[] = $line['data'];
    $hc[] = intval($line['value']);
    $user[] = $hc;
    unset($hc);
}

print_r($user);


Code was reused, but for some reason, the data table hadn't been copied across.

0

精彩评论

暂无评论...
验证码 换一张
取 消