Jump to content

How to check if a user exists?


cjx2240
 Share

Recommended Posts

This must be really simple, I don't know what I'm doing wrong

I'm trying to create a simple import for users, but it's not working. I want to check if they exist already, because I'll update their details if they do, and create them if they don't.

Can someone tell me why this is returning true for $users->get("username") when the user definitely doesn't exist? I have tried various combos, with or without quotes, using "name=", but that shouldn't matter because it should be returning false either way....

Truthfully I think you're going to point out something really stupid/basic logic I'm missing which I'm blaming on sleep deprivation

$users = wire('users');
foreach($newusers as $newuser) {
    if($users->get("$newuser->userid")) {
        echo $newuser->userid." already exists<br>";
    } else {
        // create the user
    }
}

Edit:

Why is it I frequently work these things out minutes after asking on here... ?

The answer is that $users->get(name) returns a PageArray object regardless of whether it matches a user. You have to check if that user has an ID or such instead.

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...