dpvmtdu364462 2014-05-18 20:50
浏览 32
已采纳

从1个表中选择多个随机值? PHP

I want 3 random images, first is working, but the other two just won't work. Preferably, I want to pick 2 other images then the first and the second, and the third to be all different.

Here's my code:

$content .= '<div>';
$qryFirstImage = "SELECT a.KW, a.KWKidsBeschrijving, a.Titel, b.GebruikersNaam
                      FROM tblKWKids AS a
                      LEFT JOIN tblUser AS b
                      ON a.UserID = b.UserID
                      ORDER BY RAND()";
                      if ($stmt = mysqli_prepare($dbconn, $qryFirstImage)) {
                      mysqli_stmt_bind_result($stmt, $KW, $KWKidsBeschrijving, $TitelKW, $GebruikersNaam);
                      mysqli_stmt_execute($stmt);
                      mysqli_stmt_fetch($stmt);
                      mysqli_close($dbconn);
                      }
$content .= '<img src="' . $KW . '" width="240px" height="240px" alt="' . $TitelKW . '" title="' . $TitelKW . '">';
$content .= '<h5>' . $TitelKW . ' door: ' . $GebruikersNaam . '</h5>';
$content .= '<p>' . $KWKidsBeschrijving . '</p>';
$content .= '</div>';

$content .= '<div>';
$qrySecondImage = "SELECT a.KW, a.KWKidsBeschrijving, a.Titel, b.GebruikersNaam
                      FROM tblKWKids AS a
                      LEFT JOIN tblUser AS b
                      ON a.UserID = b.UserID
                      ORDER BY RAND()";
if ($stmt2 = mysqli_prepare($dbconn, $qrySecondImage)) {
    mysqli_stmt_bind_result($stmt2, $KW2, $KWKidsBeschrijving2, $TitelKW2, $GebruikersNaam2);
    mysqli_stmt_execute($stmt2);
    mysqli_stmt_fetch($stmt2);
}
$content .= '<img src="' . $KW2 . '" width="240px" height="240px" alt="' . $TitelKW2 . '" title="' . $TitelKW2 . '">';
$content .= '<h5>' . $TitelKW2 . ' door: ' . $GebruikersNaam2 . '</h5>';
$content .= '<p>' . $KWKidsBeschrijving2 . '</p>';
$content .= '</div>';


$content .= '<div>';
$qryThirdImage = "SELECT a.KW, a.KWKidsBeschrijving, a.Titel, b.GebruikersNaam
                      FROM tblKWKids AS a
                      LEFT JOIN tblUser AS b
                      ON a.UserID = b.UserID
                      ORDER BY RAND()";
                      if ($stmt3 = mysqli_prepare($dbconn, $qryThirdImage)) {
                          mysqli_stmt_bind_result($stmt3, $KW3, $KWKidsBeschrijving3, $TitelKW3, $GebruikersNaam3);
                          mysqli_stmt_execute($stmt3);
                          mysqli_stmt_fetch($stmt3);
                      }
$content .= '<img src="' . $KW3 . '" width="240px" height="240px" alt="' . $TitelKW3 . '" title="' . $TitelKW3 . '">';
$content .= '<h5>' . $TitelKW3 . ' door: ' . $GebruikersNaam3 . '</h5>';
$content .= '<p>' . $KWKidsBeschrijving3 . '</p>';
$content .= '</div>';
  • 写回答

1条回答 默认 最新

  • dss89001 2014-05-18 21:04
    关注

    Please look at those

    php.net/mysqli_stmp::fetch

    php.net/mysqli_stmt::close

    Wrote very little code, search and much more.

    $qryFirstImage = "SELECT a.KW, a.KWKidsBeschrijving, a.Titel, b.GebruikersNaam
    FROM tblKWKids AS a
    LEFT JOIN tblUser AS b
    ON a.UserID = b.UserID
    ORDER BY RAND()";
    if ($stmt = mysqli_prepare($dbconn, $qryFirstImage)) {
        mysqli_stmt_execute($stmt);
        mysqli_stmt_bind_result($stmt, $KW, $KWKidsBeschrijving, $TitelKW, $GebruikersNaam);
        for($i = 0; ($i < 3 && mysqli_stmt_fetch($stmt)); $i++){
            $content .= '<div>';
            $content .= '<img src="' . $KW . '" width="240px" height="240px" alt="' . $TitelKW . '" title="' . $TitelKW . '">';
            $content .= '<h5>' . $TitelKW . ' door: ' . $GebruikersNaam . '</h5>';
            $content .= '<p>' . $KWKidsBeschrijving . '</p>';
            $content .= '</div>';
        }
        mysqli_stmt_close($stmt);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 深度学习残差模块模型
  • ¥20 两个不同Subnet的点对点连接
  • ¥50 怎么判断同步时序逻辑电路和异步时序逻辑电路
  • ¥15 差动电流二次谐波的含量Matlab计算
  • ¥15 Can/caned 总线错误问题,错误显示控制器要发1,结果总线检测到0
  • ¥15 C#如何调用串口数据
  • ¥15 MATLAB与单片机串口通信
  • ¥15 L76k模块的GPS的使用
  • ¥15 请帮我看一看数电项目如何设计
  • ¥23 (标签-bug|关键词-密码错误加密)