d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Php Undefined Offset
Add Reply New Topic New Poll
Member
Posts: 8,635
Joined: Dec 28 2007
Gold: 87.00
Jul 6 2012 06:36am
I have a array that contains result from sql query ( [ID]=>[value] ) and I'm getting user based information out of array ( array[$_SESSION['UserId']] ). Getting this Undefined Offset notice when there is no match for the UserId, how could I avoid getting this notice? Works fine when the UserId matches.
Member
Posts: 2,736
Joined: Nov 28 2009
Gold: 34.00
Jul 6 2012 07:03am
Post the full error message.
Post the line that makes the error happen.
Member
Posts: 8,635
Joined: Dec 28 2007
Gold: 87.00
Jul 6 2012 07:19am
At home now, but something like this:

Error message:
PHP Notice: in file x on line 19: Undefined offset: 107
Where 107 is the UserId of logged in user.

Line 19 from file x:
if($this->array[$_SESSION['UserId']]=="registered"]){
}

The problem occurs when the UserId is not found as key of array, but when found it works just as wanted.
Member
Posts: 2,736
Joined: Nov 28 2009
Gold: 34.00
Jul 6 2012 08:03am
Quote (vittujenkevat @ 6 Jul 2012 15:19)
At home now, but something like this:

Error message:
PHP Notice: in file x on line 19: Undefined offset: 107
Where 107 is the UserId of logged in user.

Line 19 from file x:
if($this->array[$_SESSION['UserId']]=="registered"]){
}

The problem occurs when the UserId is not found as key of array, but when found it works just as wanted.


https://www.google.com/search?q=is+key+array+php
Googling is so freaking hard.

This post was edited by eagl3s1ght on Jul 6 2012 08:03am
Member
Posts: 29,723
Joined: Jun 11 2007
Gold: 279.52
Jul 6 2012 03:10pm
what does your select qry look like?
Member
Posts: 8,635
Joined: Dec 28 2007
Gold: 87.00
Jul 7 2012 01:24am
Quote (eagl3s1ght @ 6 Jul 2012 16:03)


Indeed it is.


This should fix the issue: http://php.net/manual/en/function.array-key-exists.php

thanks ^^
Member
Posts: 2,736
Joined: Nov 28 2009
Gold: 34.00
Jul 7 2012 04:06am
Quote (vittujenkevat @ 7 Jul 2012 09:24)
Indeed it is.


This should fix the issue: http://php.net/manual/en/function.array-key-exists.php

thanks ^^


no shit? rofl
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll