Retrieve selected record from table

Discussion in 'PHP' started by vianca12, Jun 16, 2012.

  1. #1
    I want to retrieve selected records in my list.For example I have table advisor and partcipant, i want produce a list of participant that are registered under Advisor A.But when I write this sql statement, it display all participant records in the database.Can someone help me solve this problem?

    
    $sql="SELECT * FROM advisor
          WHERE Advisor_id='$Advisor_id'";
    
    
    Code (markup):

     
    vianca12, Jun 16, 2012 IP
  2. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #2
    1) Is the field Advisor_id numeric or character?

    2) What's the value of $Advisor_id when this line executes?

    3) Is participant one of the fields in the table advisor?
     
    Rukbat, Jun 16, 2012 IP
  3. vianca12

    vianca12 Peon

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    1)Advisor_id is character because it can be either user identification number/passport number.

    2)The value of advisor_id was the same as in the table.*Is this the answer you looking for?because i got a little confused with the question

    3)No.but advisor is one of the field in participant table.

    I hope you can help me solve this.Thank You
     
    vianca12, Jun 16, 2012 IP
  4. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #4
    Okay.

    No. What is the value, at the time the script is running, of the variable $Advisor_id? You'll have to use whatever you use for debugging to show you the value at the time that line of code is executed. If you don't use anything to debug (in which case you aren't yet at the level at which you should be writing any PHP code), you can use
    
        $sql="SELECT * FROM advisor WHERE Advisor_id='$Advisor_id'";
        echo '<br />'.$sql;
        exit;
    
    PHP:
    temporarily, to see what your query looks like.
    Your query and your stated intent aren't the same, then. You should use a JOIN to get the participants in table participant that have advisor $Advisor_id in table advisor.
     
    Rukbat, Jun 16, 2012 IP
  5. vianca12

    vianca12 Peon

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    How to use the right JOIN statement.Because I tried it and still failed.

    This is what I got when I run that sql. forum.PNG
     
    vianca12, Jun 16, 2012 IP
  6. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #6
    Then you're never setting $Advisor_id equal to anything. A variable doesn't exist in PHP until you make it be something - have it on the left side of the equal sign, as:

    $Advisor_id = <some value here>;
     
    Rukbat, Jun 16, 2012 IP