php - Duplicating the result when adding two tables -
i'm working on website categories. when user clicks link goes categories.php?id=$id
this working. since i'm using fk, have use code:
$result = mysql_query( "select * post_posts, post_cat post_cat = $id" ) or die("select error: ".mysql_error()); $num_rows = mysql_num_rows($result); if ($result) { while ($row = mysql_fetch_array($result)) { $_session['idpost_posts']=$row['idpost_posts']; echo '<div id="single-post" class="slidingdiv"> <div id="title">' . $row['post_header'] . '</div> <div id="image"><img src="' . $row['post_thumb'] . '" width="300" height="200"></div> <div id="text">' . substr($row['post_content'], 0, 500) . '<a href="page.php?id=' . $row['idpost_posts'] . '"> les mer...</a>' . ' </div> <div id="author">' . "skrevet: " . date('j. f y ', strtotime($row['post_date'])) . ' | <a href="user.php?id=' . $row['idpost_users'] . '">' . $row['user_name'] . '</a></div> <div id="post-divider"></div> </div>'; } }
the code working, stated earlier, shows same row 5 times. idea how fix this?
edit: i'm using now, , shows 2 rows now, better:
$result = mysql_query( "select distinct idpost_posts, post_content, post_header, post_thumb, post_date, idpost_users, user_name post_posts, post_cat, post_users post_cat = $id" ) or die("select error: ".mysql_error()); $num_rows = mysql_num_rows($result);if ($result) {while ($row = mysql_fetch_array($result)) { $_session['idpost_posts']=$row['idpost_posts']; echo '<div id="single-post" class="slidingdiv"> <div id="title">' . $row['post_header'] . '</div> <div id="image"><img src="' . $row['post_thumb'] . '" width="300" height="200"></div> <div id="text">' . substr($row['post_content'], 0, 500) . '<a href="page.php?id=' . $row['idpost_posts'] . '"> les mer...</a>' . ' </div> <div id="author">' . "skrevet: " . date('j. f y ', strtotime($row['post_date'])) . ' | <a href="user.php?id=' . $row['idpost_users'] . '">' . $row['user_name'] . '</a></div> <div id="post-divider"></div> </div>';
your sql "select * post_posts, post_cat post_cat = $id" performing cross cartesian join(you not want this) between post_posts , post_cat table. should defining columns should joining 2 table so.
select * post_posts, post_cat post_posts.<primary key col name> = post_cat.<foreign key col name> , post_cat = $id
where replacing <> columns define foreign key relationship in each table. if want more detailed need post foreign key constraint details including column applied , column references in other table.
Comments
Post a Comment