Rolinda Strijker
Rolinda Strijker

Reputation: 63

mysql get last row in table

I'm working on a website and I need to show the last 3 videos who were added in my mysql database. I have this piece of code but it's not working:

<?php
                $sql = "SELECT * FROM seo_videos ORDER BY id DESC LIMIT 3";
                $result = $dbcon->query($sql);
                $data = $result->fetch_assoc();
                $video = $data['link'];

                echo "<iframe class=\"video_test\" width=\"410\" height=\"305\" src=\"$video\" frameborder=\"0\" allowfullscreen></iframe>";
                echo "<iframe class=\"video_test\" width=\"410\" height=\"305\" src=\"$video\" frameborder=\"0\" allowfullscreen></iframe>";
                echo "<iframe width=\"410\" height=\"305\" src=\"$video\" frameborder=\"0\" allowfullscreen></iframe>";
            ?>

It does show 3 videos on my website, but it are the 3 same videos all with latest id. The videos are added with a youtube link in my databse. I hope somebody can help me out!

Upvotes: 0

Views: 40

Answers (1)

Michiel Pater
Michiel Pater

Reputation: 23023

Your query seems to be okay, but you need to loop through the results. Like this:

while( $data = $result->fetch_assoc() )
{
    $video = $data['link'];

    echo "<iframe class=\"video_test\" width=\"410\" height=\"305\" src=\"$video\" frameborder=\"0\" allowfullscreen></iframe>";
}

Upvotes: 1

Related Questions