Select logged in user's ID using PDO

后端 未结 1 569
时光说笑
时光说笑 2021-01-29 08:18

I am new to using PDO and was wondering how I would select the logged in user\'s id from my phpMyAdmin database.

My initialization file is...

sessio         


        
1条回答
  •  抹茶落季
    2021-01-29 08:52

    I'm assuming you want to get the users info where their id is the same as user_id

    You might do something like this;

    $query = $db->prepare('SELECT * FROM table WHERE id=:id');
    //using bindParam helps prevent SQL Injection
    $query->bindParam(':id', $_SESSION['user_id']);
    $query->execute();
    //$results is now an associative array with the result
    $result = $query->fetch(PDO::FETCH_ASSOC);
    

    I wasn't sure how user_id is set so I just used bindParam just in case.

    0 讨论(0)
提交回复
热议问题