Jump to content

Display results from multiple tables with php and mysql


chuck1971

Recommended Posts

I have two tables, one table (article_cat) has four fields ("id", "title", "feature1", and "feature2") and the other table has six fields (articles) has ("id", "cat_id", "title", "content", "feature1", and "feature2"). I am looking to display the "title" from the "article_cat" table that has "feature1" with a value of 1. And I want to display beneath that all the "title" results from the "articles" table where "feature1" has a value of 1. I have tried this a thousand ways with no luck. Here is a representation of what I want:

 

TITLE (article_cat)

title (articles)

title (articles)

title (articles)

 

I was hoping someone can show me the err of my ways. Thanks so much in advance to all those who help! Here is the flawed code I have thus far:

 

<?

include("includes/connect.php");

$query = mysql_query("SELECT * FROM articles, article_cat WHERE articles.feature1 = 1 AND article_cat.feature1 = 1 ORDER BY id ASC");

$numrows=@mysql_num_rows($query);

if($numrows != 0) {

while ($result = mysql_fetch_array($query)) {

?>

<tr>

<td align="left" valign="top">

<table width="100%" border="0" cellspacing="0" cellpadding="0">

<tr>

<td align="left" valign="top"><strong><? echo $result['article_cat.title']; ?>:</strong><br>

<strong>></strong><a href="article_details.php?id=<? echo $result['id']; ?>"><? echo $result['articles.title']; ?></a><br>

</td>

</tr>

</table>

</td>

</tr>

<? } } ?>

 

Thanks for the help, All.  I cannot express my appreciation here!

Link to comment
Share on other sites

You have to connect to a database...

ADD this line "mysql_select_db($database, $Db_connection);"

 

Example:

 

include("includes/connect.php");

mysql_select_db($database, $Db_connection);

$query = mysql_query("SELECT * FROM articles, article_cat WHERE articles.feature1 = 1 AND article_cat.feature1 = 1 ORDER BY id ASC");
$numrows=@mysql_num_rows($query);
if($numrows != 0) {
while ($result = mysql_fetch_array($query)) {
?>

 

$database variable will look something like this:

$database = "mysql_database_name_here";

 

$Db_connection will look like:

$Db_connection = mysql_pconnect($hostname, $username, $password) or trigger_error(mysql_error(),E_USER_ERROR);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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