alphamoment Posted June 1, 2014 Share Posted June 1, 2014 In my script, users login with their Username & Password. However, I'd like to be able to echo the email address used on their account. I've tried adding the email to the sessionI'm not having much luck... Here's a piece of the login code(untouched); $username = $_POST['name']; $passwd = $_POST['passwd']; $query = "SELECT name,passwd FROM users WHERE CONCAT('0x', hex(passwd)) = '{$salt}'"; $result = mysql_query($query); $login_ok = false; if(mysql_num_rows($result) > 0) { $login_ok = true; } if($login_ok) { $row = mysql_fetch_array($result, MYSQL_NUM); $_SESSION['user'] = $row; I've also tried messing around with this piece below in a few different ways but still nothing. <?php echo htmlentities($_SESSION['user']['email'], ENT_QUOTES, 'UTF-8'); ?> Any help is greatly appreciated.. Quote Link to comment Share on other sites More sharing options...
requinix Posted June 1, 2014 Share Posted June 1, 2014 Your query only retrieves the name and password. If you want the email too then you need to include that. Why are you grabbing the password? You don't need it once they've logged in. Quote Link to comment Share on other sites More sharing options...
alphamoment Posted June 1, 2014 Author Share Posted June 1, 2014 I was showing a snippet of the Login authentication process I'm using in hopes someone could help me add to that first created session the users email too. I have tried this; <?php $query= mysql_query("SELECT * FROM `users` WHERE `name` = '".$_SESSION['user']."' ")or die(mysql_error()); $account = mysql_fetch_array($query); $num = mysql_numrows($query); ?> /// <?php echo $account['email']; ?> But the results display empty :s Quote Link to comment Share on other sites More sharing options...
Solution mac_gyver Posted June 1, 2014 Solution Share Posted June 1, 2014 in the first posted code, you are fetching a numerical array - $row = mysql_fetch_array($result, MYSQL_NUM);, so even if your sql query in that code was selecting the email field, it wouldn't be available using the associative name 'email'. in the last posted code, $_SESSION['user'] contains the numerically indexed array $row from your first posted code. $_SESSION['user'][0] would contain the name. so, if you don't expect the name value to ever get changed for anyone while they are logged in, change your first code to also select the name field in the sql query and use mysql_fetch_assoc() to fetch an associative array, where you could reference the ['name'] or ['email'] fields in your code using $_SESSION['user']['name'] or $_SESSION['user']['email'] finally, the mysql_ functions are depreciated, you should be switching to mysqli or PDO database functions. Quote Link to comment Share on other sites More sharing options...
alphamoment Posted June 1, 2014 Author Share Posted June 1, 2014 Thank you for the help Mac. Saved me a bunch of time. Quote Link to comment 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.