Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End Check if user exists

  • This topic is empty.
Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #169205
    chrisburton
    Participant

    Hey, guys. As some of you know I’m working on a comment system for the Kirby CMS. I’m having trouble wrapping my head around a few things. I’m trying to figure out the best way to check if a user exists (by their user ID) and if that user exists, check to see what needs to be updated. Whether it be a profile image or their name.

    Can anyone spare a moment?

    Also, I am trying to use the Kirby classes as much as possible. See here. I believe I need to use db::select() for this.

    #169206
    __
    Participant

    best way to check if a user exists (by their user ID)

    I’d say something like select 1 from user where id=:id. Using the Kirby db class, I guess it would be something like:

    $id = "desired_user_id";
    db::select( "name_of_user_table","'1'",["id"=>$id]  );
    
    // or, if Kirby can't deal with selecting literals
    db::select( "name_of_user_table","id",["id"=>$id] );
    

    and …check to see what needs to be updated.

    What do you mean by “needs to be updated”? not filled out yet? filled out too long ago?

    Once you can qualify it, we should be able to build it into the query.

    #169207
    chrisburton
    Participant

    @traq I’m trying to compare what is in the session to what is in the database. If the UID exists in both, the session and the database, I know that user exists.

    Variable for UID in session: $user['uid'].

    So the code should be the following?:

    db::select( 'users,'1','uid'=>$user['uid'] );
    

    After that check is complete, I guess I could just automatically update the name, link and image of the user even if their profile data is the same. I need to do this because when people change their profile images or usernames, those links are no longer valid. Checking them against each other may just be time wasted. Or should I do that?

    Update: the following code echoes out to true:

    // Check if user exists
        $exists = db::select('users','1',array('uid'=>$user['uid']));
        if($exists) {
            echo "true";
        } else {
            echo "error!";
        }
    
    #169208
    __
    Participant

    I guess I could just automatically update the name, link and image of the user even if their profile data is the same.

    If you’re already making a query, then the only extra overhead would be the size of the data you’re sending. A new username or profile image url isn’t much. MySQL is smart enough to ignore updates where the info doesn’t actually change.

    The other option would be to store the old (original) data in the session as well as the user’s updated info. That way, you can compare the two and update the DB if necessary.

    example using array_diff_assoc:

    $original = ["a"=>"A","b"=>"B","c"=>"Sea"];
    $updated = ["a"=>"A","b"=>"B","c"=>"C"];
    
    $needs_update = array_diff_assoc( $updated,$original );
    // $needs_update contains:  ["c"=>"C"]
    

    edit:

    the following code echoes out to true

    Try id with an id that you know does not exist and see if it returns false. I read the docs on db::select, and for return value it just says “mixed,” so I don’t know what true means in this case. It could be our 1 cast to bool, or it could simply mean that the query didn’t return an error.

    #169209
    chrisburton
    Participant

    @traq If MySQL is smart enough to do that, then I would rather go with db::update rather than compare data. Less markup to deal with.

    I’m having trouble with this one as it isn’t making sense to me since I need to update more than one.

    Edit:

    Try id with an id that you know does not exist and see if it returns false[…]

    It echoes out false if I try and ID of 95 (random number).

    #169210
    __
    Participant

    It echoes out false if I try and ID of 95 (random number).

    I …guess… it works, then.

    I’m having trouble with this one as it isn’t making sense to me since I need to update more than one.

    One row, or one field?

    Is something wrong with

    db::update(
        $table, 
        ["name"=>$name, "link"=>$link, "image"=>$image], 
        ["id"=>$id] 
    );
    

    ?

    #169211
    chrisburton
    Participant

    @traq Actually, I forgot the syntax but looked at some old code of mine and remembered how to do it which is close to what you have.

    My code:

    db::update('users', array($user['name']=>'name',$user['image']=>'image',$user['link']=>'link'), array('uid' => $user['uid']));
    

    Edit: Oops do I have it backwards?

    #169212
    __
    Participant

    Oops do I have it backwards?

    looks right.

    BTW, sorry if it’s confusing, I just realized I’ve been using the (php 5.4+) array literal syntax in my examples.
    It’s so nice not having to write array( ... ); anymore : )

    #169213
    chrisburton
    Participant

    @traq It was in fact backwards. I just tested with my Twitter account and it does update correctly. Finally moving forward! I really appreciate your help on this one. Now just need to figure out how to join tables.

    BTW, sorry if it’s confusing, I just realized I’ve been using the (php 5.4+) array literal syntax in my examples. It’s so nice not having to write array( … ); anymore : )

    No problem at all.

    #169214
    __
    Participant

    Oh; I was looking at the arg order, not the key=>value order. Sorry.

    #169215
    chrisburton
    Participant

    That was my fault. Thanks again, man.

    #169216
    __
    Participant

    no prob!

Viewing 12 posts - 1 through 12 (of 12 total)
  • The forum ‘Back End’ is closed to new topics and replies.