使用PHP和mysqli从数据库中排除以前的值

时间:2014-05-20 17:55:19

标签: php mysql database random mysqli

所以假设我的数据库中有30个条目,我选择前7个这样的第一页:

$qryRandomGallery = "SELECT a.Titel, a.KW, a.KWKidsID, a.KWKidsBeschrijving, b.ScoreAfbeelding, c.GebruikersNaam
                        FROM tblKWKids AS A
                        LEFT JOIN tblScore AS b
                        ON a.ScoreID = b.ScoreID
                        LEFT JOIN tblUser as C
                        ON a.UserID = c.UserID
                        ORDER BY RAND()
                        LIMIT 6";
if ($stmtRandomGallery = mysqli_prepare($dbconn, $qryRandomGallery)) {
            mysqli_stmt_execute($stmtRandomGallery);
            mysqli_stmt_bind_result($stmtRandomGallery, $KWTitel, $KWURL, $KWID, $KWKiddyBeschrijving, $ScoreAfb, $USER);
            mysqli_stmt_store_result($stmtRandomGallery);
}

$qryRandomGalleryBIG = "SELECT a.Titel, a.KW, a.KWKidsID, a.KWKidsBeschrijving, b.ScoreAfbeelding, c.GebruikersNaam
                        FROM tblKWKids AS A
                        LEFT JOIN tblScore AS b
                        ON a.ScoreID = b.ScoreID
                        LEFT JOIN tblUser as C
                        ON a.UserID = c.UserID
                        ORDER BY RAND()
                        LIMIT 1";
if ($stmtRandomGalleryBIG = mysqli_prepare($dbconn, $qryRandomGalleryBIG)) {
            mysqli_stmt_execute($stmtRandomGalleryBIG);
            mysqli_stmt_bind_result($stmtRandomGalleryBIG, $KWTitelB, $KWURLB, $KWIDB, $KWKiddyBeschrijvingB, $ScoreAfbB, $USERB);
            mysqli_stmt_store_result($stmtRandomGalleryBIG);
}

然后这就是我的php

while(mysqli_stmt_fetch($stmtRandomGallery)){
$content .= '<div>';
$content .= '<a href="galerij.php?id=' . $KWID . '">';
$content .= '<img src="' . $KWURL . '" title="' . $KWTitel . '" alt="' . $KWTitel . '" class="image">';
$content .= '<h5>' . $KWTitel . ' door: ' . $USER . '</h5>';
$content .= '<p>' . $KWKiddyBeschrijving . '</p>';
$content .= '<img src="' . $ScoreAfb . '" title="Score" alt="Score" class="img">';
$content .= '</a>';
$content .= '</div>';
}
while(mysqli_stmt_fetch($stmtRandomGalleryBIG)){
$content .= '<h2>Uitgelicht werk van ' . $USERB . '</h2>';
$content .= '<a href="galerij.php?id=' . $KWIDB . '">';
$content .= '<img src="' . $KWURLB . '" title="' . $KWTitelB . '" alt="' . $KWTitelB . '" id="image">';
$content .= '<h3>' . $KWTitelB . ' door: ' . $USERB . '</h3>';
$content .= '<h4>' . $KWKiddyBeschrijvingB . '</h4>';
$content .= '<img src="' . $ScoreAfbB . '" title="Score" alt="Score" id="score">';
$content .= '</a>';
}

现在,如何从第一个stmt中排除结果,为我的BIG图像选择另一个随机图像?

1 个答案:

答案 0 :(得分:0)

简单:获取第一个查询的ID,并将其作为not in填入第二个查询:

SELECT ...
FROM yourtable
WHERE idField NOT IN (x,y,z,p,q,r)

这会将它们从第二个查询的结果中排除。