Jump to content

SELECT From where


ChompGator

Recommended Posts

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']}";
}
?>

 

 

Link to comment
https://forums.phpfreaks.com/topic/107144-select-from-where/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/107144-select-from-where/#findComment-549322
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/107144-select-from-where/#findComment-549326
Share on other sites

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 :(

Link to comment
https://forums.phpfreaks.com/topic/107144-select-from-where/#findComment-549328
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/107144-select-from-where/#findComment-549638
Share on other sites

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!

Link to comment
https://forums.phpfreaks.com/topic/107144-select-from-where/#findComment-549719
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.