Mongodb / PHP undefined index

853 views
Skip to first unread message

Meroe Kush

unread,
May 9, 2011, 6:18:19 PM5/9/11
to mongod...@googlegroups.com

Hello all,

 

 

I'm messing around with mongodb and php in my spare time and was curious how you all handled for example:

 

 

Severity: Notice

 

Message: Undefined index: birthday

 

 

Basically some documents have more "fields" that others, so when pulling up the records in php I receive the above type error on such records.  What's the best way to handle this?

Gaetan Voyer-Perrault

unread,
May 9, 2011, 8:11:51 PM5/9/11
to mongod...@googlegroups.com
I'm not 100% clear on what you are doing. Are you talking about referencing data that is already loaded into an object?

Here's a sample using isset( ).

Looks something like this:
  // Insert test data
  $collection->insert( array('A' => 1, 'B' => 4, 'C' => array("C1" => 'val', "C2" => 15) ) );
  $collection->insert( array('A' => 2, 'B' => 5 ) );

  $cursor = $collection->find();
  foreach($cursor as $data){
    if(isset($data['C'])){
      print_r($data['C']);
    }
  }

If this is not what you're looking for, would you be able to provide a similar example demonstrating your issue?

- Gates

--
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com.
To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.

Meroe Kush

unread,
May 9, 2011, 8:33:21 PM5/9/11
to mongod...@googlegroups.com

Sorry it's my fault I'm having a hard time asking my question.....

 

Let's say I do this

 

#1 -

  $collection->insert( array('A' => 2, 'B' => 5 ) );

#2

  $collection->insert( array('A' => 1, 'B' => 4, 'C' => array("C1" => 'val', "C2" => 15) ) );

 

#3

  $records = $collection->find();

  return $records

 

This will get two records

 

The first with A and B

The second with A,B,C

 

in my PHP page...

 

foreach ($records as $record)

 

echo $record ['A'];

echo $record ['B'];

echo $record ['C'];

 

 

 

}

 

I would get an undefined index on the first record for C as it doesn't exist.  In mysql no data would exist but the index would. What I'm looking to do is avoid this error.  I guess I could do....

 

 

foreach ($records as $record)

 

    if(isset($record ['A'];))  echo $record ['A'];

    if(isset($record ['B'];)) echo $record ['B'];

    if(isset($record ['C'];))  echo $record ['C'];

Gates

unread,
May 9, 2011, 8:38:38 PM5/9/11
to mongodb-user
> I guess I could do....

With no guarantees that any particular field exists, you will need to
use the isset().

Your second version of the code is correct.

- Gates
> On Mon, May 9, 2011 at 3:18 PM, Meroe Kush <whme...@gmail.com> wrote:
>
> Hello all,
>
> I'm messing around with mongodb and php in my spare time and was curious how
> you all handled for example:
>
> Severity: Notice
>
> Message: Undefined index: birthday
>
> Basically some documents have more "fields" that others, so when pulling up
> the records in php I receive the above type error on such records.  What's
> the best way to handle this?
>
> --
> You received this message because you are subscribed to the Google Groups
> "mongodb-user" group.
> To post to this group, send email to mongod...@googlegroups.com.
> To unsubscribe from this group, send email to
> mongodb-user...@googlegroups.com
> <mailto:mongodb-user%2Bunsu...@googlegroups.com> .
> For more options, visit this group athttp://groups.google.com/group/mongodb-user?hl=en.

Meroe Kush

unread,
May 9, 2011, 8:49:42 PM5/9/11
to mongod...@googlegroups.com
Thanks appreciate your input here.
Reply all
Reply to author
Forward
0 new messages