wargolchaos Posted August 18, 2009 Share Posted August 18, 2009 Goal assign user level into a session when a user logs in. login mysql code: // Verify $email = mysql_escape_string($_POST['email']); $password = md5($_POST['password']); $gUser = mysql_query("SELECT * FROM users WHERE email='".$email."' AND password='".$password."' LIMIT 1") or die(mysql_error()); $verify = mysql_num_rows($gUser); if($verify > 0){ echo '<h2>Login Complete</h2>'; }else{ echo '<h2>Login Failed</h2> <p>Sorry your login credentials are incorrect.<br /><br />'; } So what I need to do is assign a level variable, This is what I'm thinking. $level = (a db query) [-the level will be between 1 and 5-] then assign it to a session var $_SESSION["level"] = $level So I am looking to learn a reliable way to set this variable and have it stick as a session. Quote Link to comment https://forums.phpfreaks.com/topic/170900-solved-loading-mysql-data-into-a-variable/ Share on other sites More sharing options...
MadTechie Posted August 18, 2009 Share Posted August 18, 2009 Just fetch the level and assign it /*addition */ session_start(); $row = mysql_fetch_assoc($gUser); $_SESSION["level"] = $_row["level"]; /*addition complete*/ echo '<h2>Login Complete</h2>'; Quote Link to comment https://forums.phpfreaks.com/topic/170900-solved-loading-mysql-data-into-a-variable/#findComment-901378 Share on other sites More sharing options...
wargolchaos Posted August 18, 2009 Author Share Posted August 18, 2009 Its not working. Heres what the entire script looks like. <?php if(isset($_SESSION["level"])) { echo '<h3>Admin</h3> <p>This is an example of a content page!</p>'; echo '<h3>Welcome '.$_SESSION["name"].', your user level is '.$_SESSION["level"]; } else { include('db_info.php'); connectDB(); selectDB("-----"); if(isset($_POST['email']) && isset($_POST['password'])){ // Verify $email = mysql_escape_string($_POST['email']); $password = md5($_POST['password']); $gUser = mysql_query("SELECT * FROM users WHERE email='".$email."' AND password='".$password."' LIMIT 1") or die(mysql_error()); $verify = mysql_num_rows($gUser); if($verify > 0){ /*Set user level*/ $row = mysql_fetch_assoc($gUser); $_SESSION["level"] = $_row["level"]; $_SESSION["name"] = $email; /*End of set user level*/ echo '<h2>Login Complete</h2>'; echo '<h3>Welcome '.$_SESSION["name"].', your user level is '.$_SESSION["level"]; }else{ /* If login fails> */ echo '<h2>Login Failed</h2> <p>Sorry your login credentials are incorrect. <a href=\"admin.php\">Try again</a><br /><br />'; } }else{ ?> heres what my db looks like $sql ="CREATE TABLE users ( id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, username VARCHAR(50) NOT NULL, email VARCHAR(50) NOT NULL, password VARCHAR(32) NOT NULL, date DATE NOT NULL, level INT NOT NULL ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1"; Quote Link to comment https://forums.phpfreaks.com/topic/170900-solved-loading-mysql-data-into-a-variable/#findComment-901397 Share on other sites More sharing options...
roopurt18 Posted August 18, 2009 Share Posted August 18, 2009 You mistyped your variable name: $row = mysql_fetch_assoc($gUser); $_SESSION["level"] = $_row["level"]; // VARIABLE $_row IS NOT DEFINED Quote Link to comment https://forums.phpfreaks.com/topic/170900-solved-loading-mysql-data-into-a-variable/#findComment-901402 Share on other sites More sharing options...
wargolchaos Posted August 18, 2009 Author Share Posted August 18, 2009 Thanks, your pinned it. Sitting here working on this website for so long has me blind. Quote Link to comment https://forums.phpfreaks.com/topic/170900-solved-loading-mysql-data-into-a-variable/#findComment-901408 Share on other sites More sharing options...
roopurt18 Posted August 18, 2009 Share Posted August 18, 2009 Take a 5 minute break every hour. Walk around and stretch. It helps. Quote Link to comment https://forums.phpfreaks.com/topic/170900-solved-loading-mysql-data-into-a-variable/#findComment-901431 Share on other sites More sharing options...
wargolchaos Posted August 18, 2009 Author Share Posted August 18, 2009 Yeah, I should start doing that. Maybe i'll take a walk around the block or something. I've really been hard core about learning and working with PHP. Quote Link to comment https://forums.phpfreaks.com/topic/170900-solved-loading-mysql-data-into-a-variable/#findComment-901432 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.