Page 1 of 1

Foreign Key

Posted: Wed Feb 04, 2009 4:22 pm
by camdenite
Hi all,

I am trying to get the information of the foreign key in one table that is held in another. Tables blogb and blog_category.

I tried this:

$query = "SELECT blog_id, date2, heading, title FROM blogb, blog_category";

//$query = "SELECT CONCAT(pid, ', <br>', pname, ', <br>', pcontent, ', ', ptitle, '. <br /><br />') AS pages FROM tbl_pages";
$result = @mysql_query ($query); // Run the query.
$num = mysql_num_rows($result);

if ($num > 0) { // If it ran OK, display the records.

echo "<p>There are currntly $num Blog entries.</p>\n";

// Table header.
echo '<table align="center" cellspacing="0" cellpadding="5">
<tr>
<td align="left"><b>Category</b></td>
<td align="left"><b>Date</b></td>
<td align="left"><b>Title</b></td>
<td align="view"><b>View</b></td>
<td align="left"><b>Edit</b></td>
<td align="left"><b>Delete</b></td>
</tr>
';
// Fetch and print all the records.
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo '<tr>
<td align="left">' . $row['title'] . '</td>
<td align="left">' . $row['date2'] . '</a></td>
<td align="left"><a href="blog_view.php?id=' . $row['blog_id'] . '">' . $row['heading'] . '</td>
<td align="left"><a href="blog_view.php?id=' . $row['blog_id'] . '">View</a></td>
<td align="left"><a href="blog_edit4b.php?id=' . $row['blog_id'] . '">Edit</a></td>
<td align="left"><a href="delete_blog.php?id=' . $row['blog_id'] . '">Delete</a></td>
</tr>
';
}

But this just gives me the same blog displayed multiple times becase it has more than one category. I suppose I want the blog displayed once and the various categories displayed in a categories section. How would I write this? Associate one blog with many categories?

Thank you for any advice,
Colin

Re: Foreign Key

Posted: Wed Feb 04, 2009 4:42 pm
by camdenite
I got it:

// Make the query.
$query = "SELECT blog_id, date2, heading, category_id, id, title FROM blogb, blog_category WHERE category_id = id ";