ChompGator Posted May 25, 2008 Share Posted May 25, 2008 I have a script (its below) and I have it to where it's retrieving a 'rank' from the database that cooresponds to each user. User 1 = XRank User 2 = YRank User 3 = SRank But when a user logs in and clicks on his profile instead of showing just their rank, its listing all the ranks from the 'rank' column in the database table. I just want it to show that user's rank, does anyone know why that is? <?php $con = mysql_connect("***","***","****") or die('Could not connect: ' . mysql_error()); mysql_select_db("login", $con); $result = mysql_query("SELECT * FROM users"); while($row = mysql_fetch_assoc($result)){ echo "{$row['rank']}"; } ?> Quote Link to comment Share on other sites More sharing options...
ChompGator Posted May 25, 2008 Author Share Posted May 25, 2008 Sorry should be more specific, its When a user clicks on their profile to see their rank > instead of just showing their rank its listing all the ranks for all 300 users in the database. And I just want it so that it lists the rank for that specific user. Quote Link to comment Share on other sites More sharing options...
AndyB Posted May 25, 2008 Share Posted May 25, 2008 SELECT rank from users where some_field = 'something in the login that identifies the user' Quote Link to comment Share on other sites More sharing options...
msimonds Posted May 25, 2008 Share Posted May 25, 2008 Is this really your code: SELECT * FROM users If you wanting to show just the user's information and ranking, I would use their user id or some other field in your users table that makes them unique such as: "Select * From Users Where userid '{$userid}'"; Your query is showing all the data from the user table and that is why they can see eveyone else's data Hope this helps ~Mike Quote Link to comment Share on other sites More sharing options...
garry Posted May 25, 2008 Share Posted May 25, 2008 It should be something like: SELECT * FROM users WHERE id = '$userid' Quote Link to comment Share on other sites More sharing options...
ChompGator Posted May 25, 2008 Author Share Posted May 25, 2008 let me try that out, I took your script, Ill post my error here if any give me a few. Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted May 25, 2008 Share Posted May 25, 2008 You need to have some sort of way to know whose profile is being viewed. You can do that by either using a session variable for the user that holds their ID or something of the sort, or you could do it through the URL, such as: www.your-site.com/profile.php?userID=5 Then you can use either one of those variables to create your query. Also, there is no need for a while loop when you only expecting one row from the db. Quote Link to comment Share on other sites More sharing options...
ChompGator Posted May 25, 2008 Author Share Posted May 25, 2008 Thanks for all the advice, I was using <? session_start() ?> to start sessions at the top of my page then to display the name, and last name, etc...later in the page I was using <?php echo $_SESSION['name']." <?php echo $_SESSION['last_name']." And that was all working, then I tried using <?php echo $_SESSION['rank']." and for some reason its not showing the rank It will show everything else, the firstname, lastname, date of birth using <?php echo $_SESSION['what-ever-i-specifiy']." but when trying it with the rank, it wont show so then I tried using <?php $con = mysql_connect("****","***","****") or die('Could not connect: ' . mysql_error()); mysql_select_db("login", $con); $result = mysql_query("SELECT * FROM users"); while($row = mysql_fetch_array($result)){ echo "{$row['rank']}"; } ?> Then thats when it starting listing every rank of every user in the table and its still not working Quote Link to comment Share on other sites More sharing options...
phpSensei Posted May 25, 2008 Share Posted May 25, 2008 Try using a die(mysql_error()) statement. Quote Link to comment Share on other sites More sharing options...
ChompGator Posted May 25, 2008 Author Share Posted May 25, 2008 Try using a die(mysql_error()) statement. Ill give it a shot now...But it couldn't be a problem in my connection string could it? I mean the script is connecting, its just displaying all the ranks in the rank column, instead of the rank for the specific user. Quote Link to comment Share on other sites More sharing options...
phpSensei Posted May 25, 2008 Share Posted May 25, 2008 Try using a die(mysql_error()) statement. Ill give it a shot now...But it couldn't be a problem in my connection string could it? I mean the script is connecting, its just displaying all the ranks in the rank column, instead of the rank for the specific user. If you want a specific user then try $id = // some users id by $_POST or $_GET or given number $result = mysql_query("SELECT * FROM users WHERE user_id = '$id'"); I don't know what you column name is for the user id's so i called it user_id, change that if you like. That query just selects all the users, no specific value is given. Quote Link to comment Share on other sites More sharing options...
ChompGator Posted May 25, 2008 Author Share Posted May 25, 2008 Hey Everyone, Im having trouble with my sessions again - at least thats what I think - I have: <?php session_start(); ?> at the top of my page... Then Im using <?php echo $_SESSION['first_name'];?> to show the user's first name on the page and it works good. Now Im trying to use, <?php echo $_SESSION['location'];?> To show the users location (which is stored in the db) but its not retrieving their location. And Im wondering if anyone might know why? Im retrieving the users first_name and displaying it on the page and it works ok, how come it wont retrieve their location? If anyone knows why this is, any help would be great. What I did to try and fix this problem is approach it a different way and just used this script where I want their location: <?php $con = mysql_connect("***","***","***") or die('Could not connect: ' . mysql_error()); mysql_select_db("login", $con); $result = mysql_query("SELECT * FROM users"); while($row = mysql_fetch_array($result)){ echo "{$row['location']}"; } ?> But the problem with using that script is it isn't just listing the location for that user, its listing all the locations from the 'location' column in the database. So I edited it to this, and that didnt work to well either: <?php $con = mysql_connect("***","***","***") or die('Could not connect: ' . mysql_error()); mysql_select_db("login", $con); $result = mysql_query("SELECT * FROM users"); WHERE id = '$location'; echo "{$row['location']}"; } ?> Any help anyone could offer would be great - thanks! 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.