scoobybrew Posted February 2, 2011 Share Posted February 2, 2011 I want to show data for logged in user, i am using sessions to login. This is the code i already have: // Connect to server and select database. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); //this selects everything for the current user, ready to be used in the script below $result = mysql_query("SELECT id, points, ingame_points, ingame_money, ingame_items FROM members; WHERE username = $_SESSION['myusername']"); //this function will take the above query and create an array while($row = mysql_fetch_array($result)) { //with the array created above, I can create variables (left) with the outputted array (right) $points = $row['points']; $id = $row['id']; $ingame_points = $row['ingame_points']; $ingame_money = $row['ingame_money']; $ingame_items = $row['ingame_items']; } Help :-\? Link to comment https://forums.phpfreaks.com/topic/226489-show-data-for-logged-in-user/ Share on other sites More sharing options...
PaulRyan Posted February 2, 2011 Share Posted February 2, 2011 <?php // Connect to server and select database. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); //this selects everything for the current user, ready to be used in the script below $query = "SELECT id, points, ingame_points, ingame_money, ingame_items FROM members WHERE username='{$_SESSION['myusername']}' LIMIT 1"; if($doQuery = mysql_query($query)) { if(mysql_num_rows($doQuery)) { $user = mysql_fetch_assoc($doQuery); print_r($user); } else { echo 'No result returned for the query: '.$query; } } else { echo 'The following query failed: '.$query; } Try the above code, tell me if it is what you're looking for Regards, PaulRyan. Link to comment https://forums.phpfreaks.com/topic/226489-show-data-for-logged-in-user/#findComment-1169016 Share on other sites More sharing options...
scoobybrew Posted February 3, 2011 Author Share Posted February 3, 2011 Quote <?php // Connect to server and select database. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); //this selects everything for the current user, ready to be used in the script below $query = "SELECT id, points, ingame_points, ingame_money, ingame_items FROM members WHERE username='{$_SESSION['myusername']}' LIMIT 1"; if($doQuery = mysql_query($query)) { if(mysql_num_rows($doQuery)) { $user = mysql_fetch_assoc($doQuery); print_r($user); } else { echo 'No result returned for the query: '.$query; } } else { echo 'The following query failed: '.$query; } Try the above code, tell me if it is what you're looking for Regards, PaulRyan. Thanks, but that doesnt work.. it says Quote No result returned for the query: SELECT id, points, ingame_points, ingame_money, ingame_items FROM members WHERE username='' LIMIT 1 Whats wrong? Link to comment https://forums.phpfreaks.com/topic/226489-show-data-for-logged-in-user/#findComment-1169346 Share on other sites More sharing options...
marcelobm Posted February 3, 2011 Share Posted February 3, 2011 I don't know if that's all the code in case it is, you need to start the session for that page if not the session will be empty add to the top of the page <?php session_start(); Link to comment https://forums.phpfreaks.com/topic/226489-show-data-for-logged-in-user/#findComment-1169379 Share on other sites More sharing options...
scoobybrew Posted February 3, 2011 Author Share Posted February 3, 2011 Quote I don't know if that's all the code in case it is, you need to start the session for that page if not the session will be empty add to the top of the page <?php session_start(); Thanks! I added this at the top of the page and it worked Thanks alot edit: Oh and also, another question, how do i put the values in diffirent places? Link to comment https://forums.phpfreaks.com/topic/226489-show-data-for-logged-in-user/#findComment-1169381 Share on other sites More sharing options...
marcelobm Posted February 3, 2011 Share Posted February 3, 2011 hwat do you mean, which values? in which places? Link to comment https://forums.phpfreaks.com/topic/226489-show-data-for-logged-in-user/#findComment-1169383 Share on other sites More sharing options...
scoobybrew Posted February 3, 2011 Author Share Posted February 3, 2011 Quote hwat do you mean, which values? in which places? i meant the values i got for the logged in user (id, points and others) And for example, i want id at the top of the page, points at the bottom. How do i do that? Link to comment https://forums.phpfreaks.com/topic/226489-show-data-for-logged-in-user/#findComment-1169389 Share on other sites More sharing options...
marcelobm Posted February 3, 2011 Share Posted February 3, 2011 if you use the $user = mysql_fetch_assoc($doQuery); approach, you would be able to access the data simply by calling the $user['id'] at the top (of course after the query) and $user['points'] at the bottom where you want to print it out Link to comment https://forums.phpfreaks.com/topic/226489-show-data-for-logged-in-user/#findComment-1169395 Share on other sites More sharing options...
scoobybrew Posted February 3, 2011 Author Share Posted February 3, 2011 Quote if you use the $user = mysql_fetch_assoc($doQuery); approach, you would be able to access the data simply by calling the $user['id'] at the top (of course after the query) and $user['points'] at the bottom where you want to print it out It worked. Thanks again Link to comment https://forums.phpfreaks.com/topic/226489-show-data-for-logged-in-user/#findComment-1169401 Share on other sites More sharing options...
marcelobm Posted February 3, 2011 Share Posted February 3, 2011 You're welcome Link to comment https://forums.phpfreaks.com/topic/226489-show-data-for-logged-in-user/#findComment-1169405 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.