0

So I'm trying to select the username from the users table BASED ON if they are in a gang.

Vars used.:

$member_gang = mysql_fetch_assoc(mysql_query("SELECT level,gangid,rating FROM user_stats WHERE id = '" . $_SESSION['user']['id'] . "'"));
$gang_info = mysql_fetch_assoc(mysql_query("SELECT * FROM gangs WHERE id = '".$member_gang['gangid']."'"));

And here's when I want to display it.

$getmembers = mysql_fetch_assoc(mysql_query("SELECT * FROM user_stats WHERE gangid = '" . $gang_info['gangid'] . "'"));

$displaymembers = mysql_fetch_assoc(mysql_query("SELECT * FROM users WHERE id = '" . $getmembers['id'] . "'"));

echo 'One Member of this gang is: <b>'.$displaymembers['username'].' </b>:)';

(I do have db connection etc..) It's not echoing the username?!

EDIT: I am not that good at PHP, i don't understand prepared statements?

5
  • 1
    Any MySQL errors? Have you checked the intermediate results? Commented May 7, 2012 at 13:01
  • Why are you querying id like it was a atring. Is your id and gangid fields are strings? Commented May 7, 2012 at 13:03
  • echo $getmembers['id']; what do you get? Commented May 7, 2012 at 13:04
  • Using prepared statements would improve your code, I think Commented May 7, 2012 at 13:07
  • I get nothing when i echo $getmembers['id']; :( Commented May 7, 2012 at 13:13

1 Answer 1

1
$gang_info['id']

not

$gang_info['gangid']

because you wrote above

"SELECT * FROM gangs WHERE id...."

and not

"SELECT * FROM gangs WHERE gangid..."

?

And why did you set the Ids in ' '? Are they strings? When not, change them to integer, thats a better solution for primary keys.

Sign up to request clarification or add additional context in comments.

4 Comments

The row "gangid" is inside user_stats
Yeah, but have you testet the change to $gang_info['id']? What shows a var_dump() of member_gang and gang_info and the other 2 arrays?
Thats what i wrote, you must change id to: $getmembers = mysql_fetch_assoc(mysql_query("SELECT * FROM user_stats WHERE gangid = '" . $gang_info['id'] . "'")); and $displaymembers = mysql_fetch_assoc(mysql_query("SELECT * FROM users WHERE id = '" . $getmembers['gangid'] . "'")); Or change your table structure, so that you have the same names for the ids in every tabel, not one times gangid and some times id (what is a worse name for a primary key)
but wait, i want to fetch more than 1 user that is in the same gangid from the user_stats table.. ;3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.