我正在使用具有類別的網站。當用戶點擊鏈接時,它會轉到categories.php?id=$id
添加兩個表時複製結果
這是行得通的。由於我使用FK,我要使用此代碼:
$result = mysql_query("SELECT * FROM post_posts, post_cat WHERE 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>';
}
}
的代碼工作,但正如我前面所說,它顯示在同一行的5倍。任何想法如何解決這個問題?
編輯: 這就是我現在使用的東西,而現在只顯示兩行,這是更好:
$result = mysql_query("SELECT DISTINCT idpost_posts, post_content, post_header, post_thumb, post_date, idpost_users, user_name FROM post_posts, post_cat, post_users WHERE 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>';
查詢'SELECT DISTINCT' – Ken 2013-04-05 20:04:03
*強制:* mysql_ *函數將[在PHP 5.5中棄用](http://php.net/manual/en/faq.databases.php#faq。 databases.mysql.deprecated)。不建議編寫新代碼,因爲它將來會被刪除。取而代之的是,無論是[庫MySQLi](http://php.net/manual/en/book.mysqli.php)或[PDO](http://php.net/manual/en/book.pdo.php)和[是一個更好的PHP開發人員](http://jason.pureconcepts.net/2012/08/better-php-developer/)。 – 2013-04-05 20:06:07
用'INNER JOIN'寫下您的查詢。 – Rafee 2013-04-05 20:06:25