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

时间:2014-05-18 20:50:28

标签: php random mysqli

我想要3个随机图像,首先是工作,但其他两个只是工作。 优选地,我想选择2个其他图像,然后是第一个和第二个,第三个是全部不同。

这是我的代码:

$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 个答案:

答案 0 :(得分:0)

请看那些

<强> php.net/mysqli_stmp::fetch

<强> php.net/mysqli_stmt::close

写了很少的代码,搜索等等

$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);
}