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 6 of 6
  1. #1
    p@p
    p@p is offline
    New Coder
    Join Date
    Apr 2010
    Location
    Indonesia
    Posts
    71
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Help with Inner Join

    Greetings codingforums member,

    I have a problems with inner join below is my code:

    PHP Code:
    <?php
    $con 
    mysql_connect("localhost","root","boomba");
    if (!
    $con)
      {
      die(
    'Could not connect: ' mysql_error());
      }

    mysql_select_db("siap"$con);

    $query "SELECT * FROM semester1
    INNER JOIN users ON users.id = semester1.id"
    ;  
         
    $result mysql_query($query) or die(mysql_error());


    echo 
    "<table border='1' cellspacing='1' cellpadding='1' width='100%'>
    <tr>
    <th>Mata Kuliah 1</th>
    <th>Mata Kuliah 2</th>
    <th>Mata Kuliah 3</th>
    <th>Mata Kuliah 4</th>
    <th>Mata Kuliah 5</th>
    <th>Mata Kuliah 6</th>
    <th>IPK</th>
    </tr>"
    ;
    // Print out the contents of each row into a table 
    while($row mysql_fetch_array($result))
      {
      echo 
    "<tr>";
      echo 
    "<td align='center'>" $row['matakul1'] . "</td>";
      echo 
    "<td align='center'>" $row['matakul2'] . "</td>";
      echo 
    "<td align='center'>" $row['matakul3'] . "</td>";
      echo 
    "<td align='center'>" $row['matakul4'] . "</td>";
      echo 
    "<td align='center'>" $row['matakul5'] . "</td>";
      echo 
    "<td align='center'>" $row['matakul6'] . "</td>";
      echo 
    "<td align='center'>" $row['ipk'] . "</td>";
      echo 
    "</tr>";
      }
    echo 
    "</table>";

    mysql_close($con);
    ?>
    I want to inner join the users.id on the semester1.id which it's should only that id could see the results.
    Eg: if I'm on id 54 so only me could see the result, and I couldn't see with the id 55 or else.

    Anyone could help me?

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,947
    Thanks
    79
    Thanked 4,424 Times in 4,389 Posts
    There is no reason at all to use an INNER JOIN here.

    You never get any information from the users table, so why join to it?

    If you only want data from semester1 for userid 54, then just ask for ONLY that:

    Code:
    $query = "SELECT * FROM semester1 WHERE id = 54";
    If the id is being passed in a querystring from another page then just do
    Code:
    $query = "SELECT * FROM semester1 WHERE id = " . $_GET["id"];
    If it is from <form> post data, then use $_POST in place of $_GET.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    p@p
    p@p is offline
    New Coder
    Join Date
    Apr 2010
    Location
    Indonesia
    Posts
    71
    Thanks
    4
    Thanked 0 Times in 0 Posts
    It's not used <form>, I got an error:

    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1


    Do you have any idea?

  • #4
    New to the CF scene
    Join Date
    Dec 2011
    Location
    Texas
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    you need the SQL LIKE THIS
    $query = "SELECT s.*, u.* FROM semester1 s
    INNER JOIN users u ON u.id = s.id"

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,947
    Thanks
    79
    Thanked 4,424 Times in 4,389 Posts
    weddingcoo is wrong for two reasons:
    (1) You really can (but shouldn't) use just SELECT * to get all fields from all tables.
    (2) You should not be doing an INNER JOIN at all, as I said.

    ********

    DEBUG DEBUG DEBUG:
    Code:
    $query = "SELECT * FROM semester1 WHERE id = " . $_GET["id"]; // or $_POST??
    
    echo "<hr/>DEBUG SQL: " . $query . "<hr/>\n";
    
    ...
    If you can't debug, you can't program.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #6
    p@p
    p@p is offline
    New Coder
    Join Date
    Apr 2010
    Location
    Indonesia
    Posts
    71
    Thanks
    4
    Thanked 0 Times in 0 Posts
    This issue has been resolved. Please some moderator close this thread

    Thank you for people helped me out.


  •  

    Posting Permissions

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