How do I write this SQL statement to get the ad and posting? (PHP/MySQL)

Posted by ggfan on Stack Overflow See other posts from Stack Overflow or by ggfan
Published on 2010-04-07T19:00:20Z Indexed on 2010/04/07 19:03 UTC
Read the original article Hit count: 235

Filed under:
|
|

I am a little confused on the logic of how to write this SQL statement. When a user clicks on a tag, say HTML, it would display all the posts with HTML as its tag. (a post can have multiple tags)

I have three tables:

  1. Posting-->posting_id, title, detail, etc
  2. tags-->tagID, tagname
  3. postingtag-->posting_id, tagID

I want to display all the title of the post and the date added.

    global $dbc;
    $tagID=$_GET['tagID']; //the GET is set by URL

    //part I need help with. I need another WHERE statment to get to the posting table
    $query = "SELECT p.title,p.date_added, t.tagname FROM posting as p, 
    postingtag as pt, tags as t WHERE t.tagID=$tagID";

   $data = mysqli_query($dbc, $query);

  echo '<table>';
  echo '<tr><td><b>Title</b></td><td><b>Date Posted</b></td></tr>';
  while ($row = mysqli_fetch_array($data)) {         
     echo '<tr><td>'.$row['title'].'</td>';
     echo '<td>'.$row['date_added'].'</td></tr>';
     }
  echo '</table>';
}

© Stack Overflow or respective owner

Related posts about php

Related posts about mysql