我有2個數據庫表。 loppe
含有username
和loppeID
和billeder
含有loppeID
和billedeNavn
。隨機圖像PHP/SQL
我想有一個div
包含2個隨機billedeNavn
從相同的username
。我嘗試了一些東西,但我只能弄清楚如何從相同的loppeID
(用戶名)得到2 billedeNavn
。我對這一切都很陌生。所以我會感激任何幫助。
<?php
$frontimage = ("SELECT * FROM loppe RIGHT JOIN billeder ON loppe.loppeID=billeder.loppeID WHERE loppe.username = '$username' ORDER BY RAND()");
$st = $db->prepare($frontimage);
$st->execute();
$row = $st->fetch();
$loppeid = $row['loppeID'];
$loppe = $loppeid;
$sql = "SELECT * FROM billeder WHERE loppeID = :loppeID order by rand() limit 2";
$stmt = $db->prepare($sql);
$stmt->execute(array("loppeID" => $loppe));
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$fname = $row['billedeNavn'];
echo '<div class="col-xs-6 loppe-pic-outer"><img class="img-responsive loppe-pic" src="loppebilleder/'.$fname.'"></div>';
}
?>
http://stackoverflow.com/questions/4329396/mysql-select-10-random-rows-from-600k-rows-fast –