...

View Full Version : Am I joining correctly? First time joining tables.



Portland SEO
10-16-2011, 07:31 AM
They are variables as inputs, so I can't tell if it is processing correctly.


<?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 />";

}
?>

Old Pedant
10-16-2011, 08:43 AM
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:


...
$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?

Portland SEO
10-16-2011, 09:05 AM
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:


...
$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.


Unknown column 'ID.posts' in 'where clause'

But, it does exist..

Old Pedant
10-16-2011, 11:36 PM
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:


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

... 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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum