<?php
mysql_select_db($database_XXX, $XXX);
$result= mysql_query("SELECT COUNT(*) FROM news");
$total = mysql_result($result, 0, 0);
// create a random number
mt_srand((double)microtime()*1000000);
$number = mt_rand()%$total;
// get a random entry
$result= mysql_query("SELECT * FROM news LIMIT $number, 5");
$row = mysql_fetch_array($result);
?>
這是我使用的PHP代碼,它從表我想要的隨機數據拉,但我似乎無法弄清楚如何讓它不顯示當前的帖子它是上。我需要在if語句中加入嗎?如果我做它會去哪裏以及如何實施它。我唯一能做的就是使用if語句來檢查頁面上的post_id是否與發佈的post_id匹配。但我對此很陌生,我唯一能想到的就是。 if(!$ row ['post_id'] == $ _GET ['id']){ } 我不知道該做什麼。此外,如果有人知道如何或可以幫助指向正確的方向,這將是偉大的。謝謝。相關文章上張貼
這是這裏總的事情的更新。它仍然顯示已發佈帖子。希望這可以幫助。這是該頁面的php代碼。
<?php
mysql_select_db($database_xxx, $xxx);
$result= mysql_query("SELECT COUNT(*) FROM news");
$total = mysql_result($result, 0, 0);
// create a random number
mt_srand((double)microtime()*1000000);
$number = mt_rand()%$total;
// get a random entry
$result= mysql_query(sprintf("SELECT * FROM news WHERE post_id <> %d LIMIT %d, 3", $post->post_id, $number));
$row = mysql_fetch_array($result);
?>
<?php
if (! isset($_GET['id']) || (int) $_GET['id'] === 0) {
echo "Incorrect input, aborting";
exit;
}
mysql_select_db($database_xxx, $xxx);
$sql = "SELECT * FROM news WHERE post_id = " . $_GET['id'];
// a line of debug to make sure things are as expected
$query = MYSQL_QUERY($sql);
// query your table for a match with post_id
if (mysql_num_rows($query) == "1")
// if a record is found, show the info
{
$fetch = mysql_fetch_array($query); // set $fetch to have the values from the table
} else {
echo "No match in database found."; // if no match is found, display this error
}
?>
我認爲你的問題中的文本表明你已經在正確的方向思考,儘管你應該遵循@ Matt對'mysql_ *'和[PDO]的建議(http://php.net/book.pdo.php ) – kewlashu