Russia Posted March 26, 2011 Share Posted March 26, 2011 hey guys, Im trying to register a session from a login im making and for some reason its not working. here is my code: <?php session_start(); if(isset($_POST['username'])){ $username = $_POST['username']; //name of the text field for usernames $password = $_POST['password']; //likewise here just for the password //connect to the db $user = 'root'; $pswd = ''; $db = 'chat'; $conn = mysql_connect('localhost', $user, $pswd); mysql_select_db($db, $conn); //run the query to search for the username and password the match $query = "SELECT * FROM users WHERE username = '$username' AND password ='$password'"; $result = mysql_query($query) or die("Unable to verify user because : " . mysql_error()); //this is where the actual verification happens if(mysql_num_rows($result) == 1){ //the username and password match //so e set the session to true $_SESSION['username'] = $username; $_SESSION['uID'] = $result['user_id']; //$_SESSION['email'] = $result['email']; //and then move them to the index page or the page to which they need to go header('Location: index.php'); }else{ $err = 'Incorrect username / password.' ; } //then just above your login form or where ever you want the error to be displayed you just put in echo $err; } else ?> Im trying to make it so it also gets the user_id of the user logging in and creates a session for it. It works for the username part, and Im able to echo the username im logged in with, but for some reason it does want to work for the user_id part. This is what doesnt register $_SESSION['uID'] = $result['user_id']; Thanks for the upcoming help. Quote Link to comment https://forums.phpfreaks.com/topic/231784-start-a-session-name-from-a-result-from-a-login/ Share on other sites More sharing options...
PFMaBiSmAd Posted March 26, 2011 Share Posted March 26, 2011 $result['user_id'] doesn't exist. $result is the result resource from the query. You would need to fetch the row from the result resource before you could reference a column in that row. Quote Link to comment https://forums.phpfreaks.com/topic/231784-start-a-session-name-from-a-result-from-a-login/#findComment-1192554 Share on other sites More sharing options...
Russia Posted March 26, 2011 Author Share Posted March 26, 2011 How would I be able to do that? The thing is, Im SELECT *, so basically it gets all of the information from the row. Whats the way I would do it? Quote Link to comment https://forums.phpfreaks.com/topic/231784-start-a-session-name-from-a-result-from-a-login/#findComment-1192555 Share on other sites More sharing options...
PFMaBiSmAd Posted March 26, 2011 Share Posted March 26, 2011 Before you can perform database operations on your data in your application, you must learn how to execute a query and retrieve the data from that query. Here's a basic SELECT query example (if you only expect one row, you don't need to use a loop when fetching the data from the result resource) - http://w3schools.com/php/php_mysql_select.asp Quote Link to comment https://forums.phpfreaks.com/topic/231784-start-a-session-name-from-a-result-from-a-login/#findComment-1192558 Share on other sites More sharing options...
Russia Posted March 26, 2011 Author Share Posted March 26, 2011 Can you show me how to do it in my code, i am not understanding totally. Quote Link to comment https://forums.phpfreaks.com/topic/231784-start-a-session-name-from-a-result-from-a-login/#findComment-1192563 Share on other sites More sharing options...
aabid Posted March 26, 2011 Share Posted March 26, 2011 $result = mysql_query($query) or die("Unable to verify user because : " . mysql_error()); after this line add the following, $row = mysql_fetch_array($result); this will fetch one row and save it in the $row variable as array, now suppose you want to access name field in that row, so you need to simply point to the $row[name] I hope you would have got what i mean to say. Quote Link to comment https://forums.phpfreaks.com/topic/231784-start-a-session-name-from-a-result-from-a-login/#findComment-1192566 Share on other sites More sharing options...
nethnet Posted March 26, 2011 Share Posted March 26, 2011 Before this line, $_SESSION['uID'] = $result['user_id']; You need to give the $result array some actual values. Right now that variable is just a resource identifier for your mysql_query. You can use any of the mysql_fetch_array(), mysql_fetch_assoc(), or mysql_fetch_object() functions to do this. I'm more of an OOP guy so I prefer mysql_fetch_object, but for your purposes, any of them would work equally well. $assoc = mysql_fetch_assoc($result); $_SESSION['uID'] = $assoc['user_id']; Hope this helps. More about these functions: mysql_fetch_array() | mysql_fetch_assoc() | mysql_fetch_object() Quote Link to comment https://forums.phpfreaks.com/topic/231784-start-a-session-name-from-a-result-from-a-login/#findComment-1192568 Share on other sites More sharing options...
Russia Posted March 26, 2011 Author Share Posted March 26, 2011 Thanks mate, I appreciate the small code you provided. Quote Link to comment https://forums.phpfreaks.com/topic/231784-start-a-session-name-from-a-result-from-a-login/#findComment-1192579 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.