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 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Am I joining correctly? First time joining tables.

    They are variables as inputs, so I can't tell if it is processing correctly.

    PHP Code:
    <?php
    include 'connect.php';

    $jc1 $_POST['jc1'];
    $jc2 $_POST['jc2'];


    $jt1 $_POST['jt1'];
    $jt2 $_POST['jt2'];

    $w $_POST['w'];


    $queryt1 "SELECT * FROM `$jt1`";
    $result1 mysql_query($queryt1);

    $qjoin "SELECT * FROM $jt1 JOIN $jt2 ON $jt2.$jc2=$jt2.$jc1 WHERE $w=$jt1.$w";
    $jq mysql_query($qjoin) or die(mysql_error());
    $c1 $jq[$jc1];
    $c2 $jq[$jc2];
    while(
    $row mysql_fetch_array($jq)){
        echo 
    $row[$jc1]. " - "$row[$jc2];
        echo 
    "<br />";

    }
    ?>

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,084
    Thanks
    80
    Thanked 4,552 Times in 4,516 Posts
    Wow! You would really have a situation where you'd be joining a pair of tables specified by input on fields also specified by inputs??? Seems like a too-loose database design.

    But anyway, to see what you are actually doing, simply debug:
    Code:
    ...
    $qjoin = "SELECT * FROM $jt1 JOIN $jt2 ON $jt2.$jc2=$jt2.$jc1 WHERE $w=$jt1.$w"; 
    echo "<hr>DEBUG SQL: " . $qjoin . "<hr/>\n";
    ...
    What does that echo out to you?
    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
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    Wow! You would really have a situation where you'd be joining a pair of tables specified by input on fields also specified by inputs??? Seems like a too-loose database design.

    But anyway, to see what you are actually doing, simply debug:
    Code:
    ...
    $qjoin = "SELECT * FROM $jt1 JOIN $jt2 ON $jt2.$jc2=$jt2.$jc1 WHERE $w=$jt1.$w"; 
    echo "<hr>DEBUG SQL: " . $qjoin . "<hr/>\n";
    ...
    What does that echo out to you?
    LOL. It's spitting out the right variables; however, I am getting this.

    Code:
    Unknown column 'ID.posts' in 'where clause'
    But, it does exist..

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,084
    Thanks
    80
    Thanked 4,552 Times in 4,516 Posts
    Pardon me for doubting you, but...

    So you *DO* have a table named ID??? That's unusual, but certainly possible.

    And you say that you do have a column named posts in that table?

    Just to be 100% sure, use a MySQL tool of some kind and issue this query:
    Code:
    DESCRIBE ID;
    That will dump out a brief version of the table. Copy/paste the results here.

    ************

    Since you chose not to do as I asked, I can't see what that debug showed you. But I would have to assume that $jt1 is ID and that $w is posts.

    Which means your WHERE clause in the echo looked like
    Code:
       ... WHERE posts = ID.posts
    I'm more than curious: What is that *FIRST* posts supposed to refer to???

    Is it supposed to refer to the field posts in the *other* table? If so, why isn't it part of the ON condition? I really can't make sense of your WHERE.
    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.


  •  

    Posting Permissions

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