0

Hi I have spent most of the day on this and turn to you in the hope someone can help. I need to generate a random display 5-10 embedded youtube vids on a page without duplication. I can get the videos to display (but all of the same artist) when I refresh it displays a new set of videos but again all of the same artist. I know the answer is probably very simple but I just can't see it.

Any help or pointers would be most appreciated.

The initial php code is:

$result = mysql_query("SELECT Youtube FROM artist ORDER BY RAND() LIMIT 5");
$test = mysql_fetch_array($result);
if (!$result) 
{
    die("Error: Data not found..");
}
$Youtube=$test['Youtube'];                  

With the output being:

<div class="ws_images"><ul>
  <iframe width="300" height="300" src="https://www.youtube.com/embed/<?       php echo $Youtube ?>" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
  <iframe width="300" height="300" src="https://www.youtube.com/embed/<?php echo $Youtube ?>" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
  <iframe width="300" height="300" src="https://www.youtube.com/embed/<?php echo $Youtube ?>" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
  <iframe width="300" height="300" src="https://www.youtube.com/embed/<?php echo $Youtube ?>" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
  <iframe width="300" height="300" src="https://www.youtube.com/embed/<?php echo $Youtube ?>" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
</ul></div>
Niels
  • 1,005
  • 1
  • 8
  • 18

2 Answers2

0

This should work. I changed your code and also reduced it by a while loop.

<?php 
$mysqli = new mysqli("127.0.0.1", "root", "password", "database name");
$mysqli->set_charset('utf8mb4'); //optional for when you use utf8mb4 charset (recommended) 


$getYouTubeLinks = "SELECT DISTINCT Youtube FROM artist ORDER BY RAND() LIMIT 5";
$getRows = $mysqli->query($getYouTubeLinks);


if($getRows->num_rows > 0) {
  while($row = $getRows->fetch_assoc()) {
    $iframes .= '<iframe width="300" height="300" src="https://www.youtube.com/embed/'.$row['Youtube'].'" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe><br>';
    }
  }

  ?>

  <div class="ws_images"><ul>
    <?=$iframes;?>
  </ul></div>
Niels
  • 1,005
  • 1
  • 8
  • 18
  • Hi thanks for replying, unfortunately with your code in place I get: Notice: Undefined variable: mysqli Fatal error: Call to a member function query() on null the line it refers to is this piece of code. $getRows = $mysqli->query($getYouTubeLinks); Really do appreciate the help – Andrew Cole May 25 '18 at 08:56
  • hey there, can you show me how you connect to your database without your credentials? Because the `$mysqli` variable is probably not set in your environment Also you should switch to mysqli. The i stands for improved and it's more secure. – Niels May 25 '18 at 09:55
  • connect_error) { die("Connection failed: " . $conn->connect_error); } ?> – Andrew Cole May 25 '18 at 11:26
  • I updated my answer, try it now with your own credentials. – Niels May 25 '18 at 11:35
0

$result = mysql_query("SELECT DISTINCT Youtube FROM artist ORDER BY RAND() LIMIT 5");

Mysql Documentation.

trun4o
  • 1
  • 2