Thorben Lüpkes Thorben Lüpkes - 2 months ago 14
MySQL Question

Random Video via MySQL

I have a MySQL database in which i store the URL to 8 videos. What i want to do know is display them random in a slider. It's alls working until now, besides that the Video is not random but all the time the same, which means:
The video gets randomly picked from the database, but each time i refresh the page, it's 8 times video number 1, next refresh is 8 times number 6 etc.

<?php $mysqli = new mysqli("####", "####", "###", "clearchannel");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}

$res = $mysqli->query("SELECT EmbedURL FROM Videos where location_id='2' ORDER BY RAND() LIMIT 3");
$vid = $res->fetch_assoc();
?>
<ul class="bxslider">
<li><iframe src="<?php echo $vid['EmbedURL']; ?>" frameborder="0" autoplay="0" allowfullscreen></iframe></li>
<li><iframe src="<?php echo $vid['EmbedURL']; ?>" frameborder="0" autoplay="0" allowfullscreen></iframe></li>
<li><iframe src="<?php echo $vid['EmbedURL']; ?>" frameborder="0" autoplay="0" allowfullscreen></iframe></li>
<li><iframe src="<?php echo $vid['EmbedURL']; ?>" frameborder="0" autoplay="0" allowfullscreen></iframe></li>
<li><iframe src="<?php echo $vid['EmbedURL']; ?>" frameborder="0" autoplay="0" allowfullscreen></iframe></li>
<li><iframe src="<?php echo $vid['EmbedURL']; ?>" frameborder="0" autoplay="0" allowfullscreen></iframe></li>
<li><iframe src="<?php echo $vid['EmbedURL']; ?>" frameborder="0" autoplay="0" allowfullscreen></iframe></li>
<li><iframe src="<?php echo $vid['EmbedURL']; ?>" frameborder="0" autoplay="0" allowfullscreen></iframe></li>
</ul>


?>

You can see the result here: http://134.255.234.196/school/CC/

Thanks a lot!

Answer

change your code with this new one:

1st. you r limiting your results to 3 urls from db, ORDER BY RAND() LIMIT 3" change it to 8 if you want 8 different vidios,

<?php
$mysqli = new mysqli("####", "####", "###", "clearchannel");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
      }                      

$res = $mysqli->query("SELECT EmbedURL FROM Videos where location_id='2' ORDER BY RAND() LIMIT 8");

 echo'<ul class="bxslider">';

while($url = $res->fetch_assoc()){
echo '<li><iframe src="'.$vid['EmbedURL'].'" frameborder="0" autoplay="0" allowfullscreen></iframe></li>'; 
}?>
                    </ul>

EDIT: a } was missing at the end.