Need to store the user_id in a session

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Icethrill
Forum Newbie
Posts: 13
Joined: Fri Jan 02, 2009 9:55 am

Need to store the user_id in a session

Post by Icethrill »

At the moment this is a simple register form. Its working properly and everything except that I need to get the user_id from the database and store it in a session. At the moment all I save in the sessions is if your logged in and the username. But I need the user_id from the database to connect it to other tables in the database. I havent programmed the best security or anything its probably pretty incomplete so forget about that atm. I just want some suggestions how I could get the user_id in a session? I am stuck at the moment. :(

Anyway here is the code where I set the sessions.

Code: Select all

$query = "SELECT user_id FROM user WHERE username='{$anvandarnamn}' AND password='{$password}'";
        $resultLogin = mysql_query($query);
        confirm_query($resultLogin);
    
        if(mysql_num_rows($resultLogin) == 1){
            $_SESSION['inloggad'] = true; //Sätter SESSIONen så man blir inloggad
            $_SESSION['username'] = $anvandarnamn; //Stoppar in användarnamnet så vi kan skriva ut det på skärmen när man är inloggad
            redirect_to("index.php");
        }else{
            $felLogin = "Du skrev in fel användarnamn eller lösenord!"; //Visar texten om man skrev fel username eller password
        }
Please excuse the swedish comments I have made, just ignore them. There might be an easy solution for this but I aint that experienced programmer.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Re: Need to store the user_id in a session

Post by Burrito »

you'll have to loop over your $resultLogin resource and get the ID from the array.

you can do that using if($row = mysql_fetch_assoc($resultLogin)) and get rid of your mysql_num_rows() check.
Icethrill
Forum Newbie
Posts: 13
Joined: Fri Jan 02, 2009 9:55 am

Re: Need to store the user_id in a session

Post by Icethrill »

Burrito wrote:you'll have to loop over your $resultLogin resource and get the ID from the array.

you can do that using if($row = mysql_fetch_assoc($resultLogin)) and get rid of your mysql_num_rows() check.
Ok thanks! Gonna give it a try :D
Post Reply