Jump to content

[SOLVED] Match fields in 2 mysql tables


tqla

Recommended Posts

Hello. I have two tables in my MySQL db.

 

One table is called "content" and the other is called "category".

 

A field in the "content" table is called "category_id" and that entry matches the "id" field of the "category" table.

 

The "category" table also has a "title" field.

 

I wish to echo the "title" field in the "category" table where the "category_id" in the "content" table matches the "id" field in the "category" table.

 

??? Please help! Thanks! 

 

oh, I don't need the db connection stuff, just the syntax for the above.

 

 

Link to comment
https://forums.phpfreaks.com/topic/138647-solved-match-fields-in-2-mysql-tables/
Share on other sites

Thank you Mark, that will help me pull all matching id's from the DB - I need to do that too at some point. But what I need now is more simple.

 

In the code below there are two sections - Categories (top) and Content (btm). In the Content part there is a column called Category. That number matches the actual ID in the Categories section above it.

 

Is there some php code that I can use to change that number to state the category Title from the Category section above it? Something to look at the category column in the Content (btm) section ( $row['category_id'] ) and match it to the ID of the Categories (top) section ($row2['id']) and then replace that number with the Title ($row2['title'])? 

 

<?php
include('includes/connnection.inc.php');
$sql = 'SELECT * FROM content ORDER BY created DESC';
$result = mysql_query($sql) or die(mysql_error());
$sql2 = 'SELECT * FROM category ORDER BY title ASC';
$result2 = mysql_query($sql2) or die(mysql_error());
?>
<body>
<h1>Categories</h1>

<table cellspacing="10">
    <tr>
        <th align="left">Title</th>
	<th> </th>
	<th> </th>
    </tr>
<?php
while($row2 = mysql_fetch_assoc($result2)) {
?>

    <tr>
        <td align="left"><?php echo $row2['title']; ?></td>
        <td> </td>
    </tr>
<?php } ?>
</table>

<h1>Content</h1>

<table cellspacing="10">
    <tr>
        <th align="left">Created</th>
        <th> </th>
        <th align="left">Updated</th>
        <th> </th>
        <th align="left">Title</th>
	<th> </th>
        <th align="left">Category</th>
	<th> </th>
        <th align="left">Active</th>
	<th> </th>
	<th> </th>
    </tr>
<?php
while($row = mysql_fetch_assoc($result)) {
$row['created'] = date("m/d/y g:ia", strtotime($row['created']));
$row['updated'] = date("m/d/y g:ia", strtotime($row['updated']));


if ( $row['active'] == true ) {
	$active = "Yes";
} else {
$active = "No";
}


?>

    <tr>
        <td align="left"><?php echo $row['created']; ?></td>
        <td> </td>
        <td align="left"><?php echo $row['updated']; ?></td>
        <td> </td>
        <td align="left"><?php echo $row['title']; ?></td>
        <td> </td>
        <td align="left"><?php echo $row['category_id']; ?></td>
        <td> </td>
        <td align="left"><?php echo $active; ?></td>
        <td> </td>

    </tr>


<?php } ?>
</table>
</body>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.