...

View Full Version : Help with Inner Join



p@p
12-25-2011, 09:54 PM
Greetings codingforums member,

I have a problems with inner join below is my 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?

Old Pedant
12-27-2011, 04:49 AM
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:



$query = "SELECT * FROM semester1 WHERE id = 54";

If the id is being passed in a querystring from another page then just do


$query = "SELECT * FROM semester1 WHERE id = " . $_GET["id"];

If it is from <form> post data, then use $_POST in place of $_GET.

p@p
12-27-2011, 08:22 AM
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?

weddingcoo
12-27-2011, 10:07 AM
you need the SQL LIKE THIS
$query = "SELECT s.*, u.* FROM semester1 s
INNER JOIN users u ON u.id = s.id"

Old Pedant
12-27-2011, 09:18 PM
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:


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

p@p
03-13-2012, 07:44 PM
This issue has been resolved. Please some moderator close this thread :)

Thank you for people helped me out.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum