Results 1 to 4 of 4

Thread: Working with fetch_arrow and queries

  1. #1
    Join Date
    Dec 2008
    Location
    Buenos Aires
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Working with fetch_arrow and queries

    Hello everyone!

    This is my first post, I've been reading the forums and learning php & mysql for some time. I'm stuck with a doubt, I hope and appreciate if you can help me.

    I'm running a web site using a database. To retrieve user's info I use:

    $UsersQuery = mysql_query("SELECT * FROM users WHERE id=$id");

    To get their usernames, registered date, etc. And I print that info with:

    $user = mysql_fetch_assoc($UsersQuery)
    $user['registered], etc

    That works great, but when I want to retrieve who invited the user (invited_by, its an INT)

    I tried something like $user['invited_by'], of course it will retrieve the INT of the inviter user (user id), but how can I make it to print the username of that inviter?

    I can do something like

    $invited_by = mysql_query("SELECT username FROM users WHERE id=$user[invited_by]");
    $inviter = mysql_fetch_array($invited_by);

    And I print it like:

    $inviter['username']

    But I'm using another query, I don't know if that's right because I already stored all the user's info in $user. Is there an easier way to do it?

    Thanks for your help/time and excuse my noob question.

  2. #2
    Join Date
    Nov 2006
    Location
    Northeast USA
    Posts
    408
    Thanks
    8
    Thanked 30 Times in 28 Posts

    Default

    Well, If you only stored the user-who-invited-the-current-user's UID number, and not their actual name, then there is no way that I can directly see you being able to get the user name. I'm not too big with SQL, some other forum users are more experienced, but logicaly in one query, it's not possible. You could set up an easy function which would save you time like:
    PHP Code:
    function inviter(){
    $invited_by mysql_query("SELECT username FROM users WHERE id=$user['invited_by']");
    return 
    mysql_fetch_array($invited_by);

    and call it directly after you MySQL:
    PHP Code:
    $_GLOBALS['user'] = $user;
    $inviterinviter(); 
    Not sure if it would really make it any more streamline, but if you needed to do multiple users, a function would be the best way to go.
    -Ben -- THE DYNAMIC DRIVERS
    My Links: My DD Profile||My Youtube Video Tutorials||DD Helping Coders||DD Coders In Training
    I told my client to press F5, the client pressed F, then 5, *facepalm*

  3. #3
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    I'd suggest a single function called getuserinfo($id). You can embed this in itself if you want or just call it twice.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  4. #4
    Join Date
    Nov 2006
    Location
    Northeast USA
    Posts
    408
    Thanks
    8
    Thanked 30 Times in 28 Posts

    Default

    Basically this could be done in one function, and it would save time:
    PHP Code:
    function getUserInfo($id){
    $UsersQuery mysql_query("SELECT * FROM users WHERE id=$id");
    $user mysql_fetch_assoc($UsersQuery);
    $invited_by mysql_query("SELECT username FROM users WHERE id=$user['invited_by']");
    $user['invited_by_name'] = mysql_fetch_array($invited_by);
    return 
    $user;

    To use the name of the person who invited the current user, use:
    PHP Code:
    $usergetUserInfo($_SESSION['id']); // where $_SESSION['id'] is set from login.
    echo $user['invited_by_name']; // displays the inviter's name. 
    -Ben -- THE DYNAMIC DRIVERS
    My Links: My DD Profile||My Youtube Video Tutorials||DD Helping Coders||DD Coders In Training
    I told my client to press F5, the client pressed F, then 5, *facepalm*

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •