【问题标题】:Excluding previous values from database using PHP and mysqli使用 PHP 和 mysqli 从数据库中排除以前的值
【发布时间】:2014-07-09 02:58:30
【问题描述】:

假设我的数据库中有 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 图像选择另一个随机图像?

【问题讨论】:

    标签: php mysql database random mysqli


    【解决方案1】:

    简单:获取第一个查询的 ID 并将它们作为 not in 填充到第二个查询中:

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

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

    【讨论】:

    • 所以例如我将使用这段代码; 'FROM tbKWKids WHERE KWKidsID NOT IN ('.$stmtRandomGallery.','.$stmtRandomGalleryBIG.')'
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-04
    • 2020-10-06
    相关资源
    最近更新 更多