2

I had to build a form with a select field populated with all the users registered in database. There are 10 users that I can see in admin>people.

To get all the users, I used entity_load('user').

But in my select, I have one empty option in addition to my 10 users.

I checked in my database, and I found this empty UID=0 record:

enter image description here

I searched the Internet but I didn't find a satisfying answer. I read something about Anonymous user but I didn't get it.

  • Is this record important?
  • Does it have to be there?
  • If yes, I'll do some trick to ignore it in my select.
  • If not, can I just delete it?

Thank you in advance for this little trivial question.

4

Is this record important?

Yes, it prevents some queries from failing if it's anonymous user who caused them.

Does it have to be there?

There is a possibility site would stay browsable you remove it... but it would mess your login functionality and may prevent sessions from persisting.

If yes, I'll do some trick to ignore it in my select.

Consider using EntityFieldQuery. Omit UIDs lower than 1, or select only users with name, etc.

7
  • Thanks. I was pretty sure that it was there for something :D No problem for my query, I'll just test the UID in my foreach where I populate my select's options. PS: I can accept your answer in 4 minutes ;)
    – Djouuuuh
    Sep 5 '14 at 8:00
  • 1
    "Have to" isn't a bit strong...without the anon user record authentication/session doesn't work. It's pretty much fundamental to a working Drupal install.
    – Clive
    Sep 5 '14 at 8:55
  • @Clive better now?
    – Mołot
    Sep 5 '14 at 9:01
  • I think it's fairer to say "you can still browse the site, but you can't login". Or more accurately, you can login, but that session won't persist with subsequent requests. If it's ever happened to you it's very frustrating till you work out what the problem is - then you never, ever forget it!
    – Clive
    Sep 5 '14 at 9:02
  • 1
    @Clive OK, fixed
    – Mołot
    Sep 5 '14 at 9:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.