Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    May 2009
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Display data from another table help

    Struggling to get my head around this code,

    I have two tables linked with a compound key table (applicants), the two tables I am using are job_index table and users table i am trying to get all the users who have 'applied' for job_id - 1.

    PHP Code:
      $query "SELECT * FROM applicants WHERE job_id='1'";
    $result mysql_query($query) or die ("Error in query");

    if (
    mysql_num_rows($result)>0)
     { 
       while (
    $row = @ mysql_fetch_array($result)) {
    print 
    $row["user_id"];
    $user_id $row["user_id"];

      
    $query "SELECT * FROM users WHERE id='$user_id'";
    $result mysql_query($query) or die ("Error in query");

    if (
    mysql_num_rows($result)>0)
     { 
       while (
    $row = @ mysql_fetch_array($result)) {

    print 
    "<b>" .$row["id"] . "</b><br/><br/>";
    print 
    "<b>" .$row["email"] . "</b><br/><br/>";
       }}
        
    }} 
    The result gets me three user_id's linked from the user table, but only ID - 1 is displayed

    PHP Code:
    11

    administrators

    2


    Any ideas?
    Last edited by d.fanning6388; 05-06-2009 at 09:58 PM.

  • #2
    GŁtkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    You are overwriting the $result of the first query with the second one there. Give it another name.

  • Users who have thanked venegal for this post:

    d.fanning6388 (05-06-2009)

  • #3
    New to the CF scene
    Join Date
    May 2009
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thanks for quick reply, i've amended the code to this, still causing an issue though.

    PHP Code:
      $query "SELECT * from applicants WHERE job_id = '1'";
    $result mysql_query($query) or die ("Error in query");

    if (
    mysql_num_rows($result)>0)
     { 

       while (
    $row = @ mysql_fetch_array($result)) {
          
    print 
    "<b>" .$row["user_id"] . "</b><br/><br/>";
    $user_id $row["user_id"];


    $query_new "SELECT * from users WHERE id = '".$user_id."'";
       
    $result_new mysql_query($query_new) or die ("Error in query");

    if (
    mysql_num_rows($result_new)>0)
     { 
       while (
    $row_new = @ mysql_fetch_array($result_new)) {
    print 
    "<b>" .$row_new["email"] . "</b><br/><br/>";
    print 
    "working<br/><br/>";
       }}

       }} 
    I've used an extra line of code to output 'working' in the 2nd loop, only seems to use the loop once even though its called 3 times in the 1st loop

    PHP Code:
    1

    administrators

    working

    2



  • #4
    GŁtkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    That seems to suggest, that those users in fact do not exist. Are you sure they are there?

  • Users who have thanked venegal for this post:

    d.fanning6388 (05-06-2009)

  • #5
    New to the CF scene
    Join Date
    May 2009
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Thumbs up



    Thanks, how stupid do I feel now, I had deleted the records on the table.

    Didn't even think too look, I new it was something basic!!

    Thanks Again!!!


  •  

    Posting Permissions

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