我有一个MySQL数据库,我将URL存储到8个视频中。我想知道的是在滑块中随机显示它们。它一直工作到现在为止,除了视频不是随机的,但总是相同的,这意味着视频是从数据库中随机挑选的,但每次刷新页面时,它都是视频数量的8倍1,下次刷新是8次等8次。
<?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>
答案 0 :(得分:0)
你需要在结果上加上:
<ul class="bxslider">
<?php
while($vid = $res->fetch_assoc()){ ?>
<li><iframe src="<?php echo $vid['EmbedURL']; ?>" frameborder="0" autoplay="0" allowfullscreen></iframe></li>
<?php } ?>
</ul>
答案 1 :(得分:0)
使用这个新代码更改您的代码:
第一。如果你想要8个不同的视频,你可以将你的结果限制为3个网址,ORDER BY RAND() LIMIT 3"
将其更改为8
<?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>
编辑:最后缺少}
。