PDA

View Full Version : Querying two tables...


poisedforflight
11-13-2005, 08:54 AM
I am trying to pull some information out of two tables and format it in an html table. This is my code, it tells me the query fails.

<?
include 'db.php';

$topicId=$_REQUEST['topicId'];

dbConnect("jmcclure_MyNotes");
$query = "topicName, topicBody FROM topicsTable, entryTable WHERE topicParent = $topicId";
$result = mysql_query($query) or die ("Query 'A' Failed");
?>
<html>
<head>
<title>MyNotes</title>
</head>

<body>
<table>

<?
while($row = mysql_fetch_assoc($result)){
print "<tr>";
print "<td>";
print $row['topicName'];
print "</td></tr>";
print "<tr>";
print "<td>";
print $row['topicBody'];
print "</td></tr>";
}
print "</table>";

?>

</body>
</html>


--------------------------------------------------------------------

Any ideas?

Brandoe85
11-13-2005, 10:21 AM
You don't have the select statement in your query, also, put in mysql_error() it'll help with debugging:

$query = "select topicName, topicBody FROM topicsTable, entryTable WHERE topicParent = $topicId";
$result = mysql_query($query) or die ("Query 'A' Failed " . mysql_error());


Good luck;

Velox Letum
11-14-2005, 12:43 AM
You also probably need to rewrite that query into proper join format, as the selected tables and WHERE condition don't have a prefix for the table, however without a table format it's rather difficult to write the correct syntax. The way it is now, if it has any fieldnames in common it'll come back with an error, which is why the table must be defined for a JOIN. Example:

SELECT a.userid, a.id, u.name FROM activation a, users u WHERE u.id = 10 AND a.userid = u.id;

This query selects the userid stored in the activation table, as well as the activation id, while the name comes from the users table. Since the users table id has to be 10, and activation table userid has to be equal to the users table id, it selects the correct data.