• PHP/MySQL Problem: Retrieve data from users table

    WinMan Member

    I’m developing a browser based MMORG and I’m stuck at a point… I retrieve data from users table, and some of the data are ID’s of other users (for example testaments… user 1 set 2 as testament). Is there any way to retrieve the username of user 2 without any other query?

  • ShikhaTan Member

    Perhaps its an idea to post the query you’re using. And important in this case seems to be, is the user a unique value ?

    Also, Im not fully sure I understand your problem but I think you’re getting more back then you’d like ?

    SELECT username FROM Users WHERE User=2

    Maybe the above statement would work ? It only returns the username of user2 in the table Users.

  • Adan Member
    SELECT a.*, b.username AS testamentUser FROM users AS a LEFT JOIN users AS b ON a.testament=b.id WHERE a.id=1
  • SapnaVishwas Member

    why would you do a join on the same table i’m assuming the id and username are both in the same row if that is not the case your user table needs to be redesigned but you could easily get all data from the table including id and username in one query by doing

    SELECT * FROM users WHERE ID = {ID}

    and depending on what you using to query (please don’t say mysql_*) just fetch it as an object or assoc and you’ll have all the data in one query.

Viewing 3 reply threads
  • You must be logged in to reply to this topic.
en_USEnglish