silverglade Posted February 20, 2012 Share Posted February 20, 2012 Hi , I am trying to get the username from the database and put it into a session variable to be used throughout the website. Also, on the profile page, I want to pull out all the data for that "username" and echo it out onto the screen. for the user that is logged in. But I don't know how to do this. any help greatly appreciated. here is how I am trying to put the current logged in user into a session variable. //$username is the current logged in user $query = mysql_query("SELECT username FROM users WHERE username = '$username')"); $_SESSION['currentUser']=$query; now on the profile page, I have no idea how to use $_SESSION['currentUser'] to output the row data for the currentUser like "name" "address" "date of birth" etc. Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted February 20, 2012 Share Posted February 20, 2012 You're storing a query result resource in $_SESSION['currentUser'], not values. Quote Link to comment Share on other sites More sharing options...
silverglade Posted February 20, 2012 Author Share Posted February 20, 2012 thank you. I don't know how to code it. I am trying to put the current logged in user into a session variable to user on future pages. and when I load up the profile page, I want to use mysql to echo out the row of data for that user. Is there something I can google to learn to do this. I tried "echoing out user data" or "outputting user data" and I can't find anything. When I try to find echoing out table data tutorials, they just show you how to output "*" data in the table. Not specific rows based on username or user id. Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted February 20, 2012 Share Posted February 20, 2012 You just do it the same way you would assign a value from any other db query, after using one of the mysql_fetch_assoc/row/array functions. Quote Link to comment Share on other sites More sharing options...
silverglade Posted February 20, 2012 Author Share Posted February 20, 2012 thank you. I made the profile upload page "logged in only". and it works. But it does not echo out the data for the current logged in user. Here is the code I am trying to echo out the current user's data on that page. $username is declared and set in another file. I used "include" to use it here. EDIT: i spoke too soon. It does output some table data, but not formatted yet, and it looks like it outputs ALL the tables data instead of just the user. but at least its a start!! any more advice appreciated. thanks so far. $sql = "SELECT username as firstName,lastName,birthday,country,city, state, aboutMe FROM users WHERE username = '$username'"; $result = mysql_query($sql); if (!$result) { echo "Could not successfully run query ($sql) from DB: " . mysql_error(); exit; } if (mysql_num_rows($result) == 0) { echo "No rows found, nothing to print so am exiting"; exit; } // While a row of data exists, put that row in $row as an associative array // Note: If you're expecting just one row, no need to use a loop // Note: If you put extract($row); inside the following loop, you'll // then create $userid, $fullname, and $userstatus while ($row = mysql_fetch_assoc($result)) { echo $row["firstName"]; echo $row["lastName"]; echo $row["birthday"]; echo $row["country"]; echo $row["city"]; echo $row["state"]; echo $row["aboutMe"]; } mysql_free_result($result); 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.