0

I have 2 database tables. loppe containing username and loppeID, and billeder containing loppeID and billedeNavn.

I would like to have a div containing 2 random billedeNavn from the same username. I have tried some things but I can only figure out how to get 2 billedeNavn from the same loppeID (username). I'm pretty new to all this. So would me grateful for any help.

<?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>';
    }
?>
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Kim Anov
  • 53
  • 1
  • 6

1 Answers1

0

Have you tried this change your
$sql = "SELECT * FROM billeder WHERE loppeID = :loppeID order by rand() limit 2";?
to this
$sql = "SELECT * FROM billeder WHERE loppeID = '.$loppeid.' order by rand() limit 2";

A good way to figure out what is going on when using php and database is after you create your query do something like
$sql = "SELECT * FROM billeder WHERE loppeID = '.$loppeid.' order by rand() limit 2"; die($sql);

So with this you will see what the database is really querying and you can check for in your sql string or query.

Guizinhobeback
  • 86
  • 1
  • 14