Dusty Hawkins Dusty Hawkins - 7 months ago 28
PHP Question

use a PHP variable as the source in an iframe to play a youtube video

I have spent the better part of three days on this problem. I've tried several solutions that I've found on this site but with little success. What I'm trying to do is use a PHP variable to play a youtube video in an iframe. I'm also trying to run two MySQL queries with the same input. Here is my code as it sits right now. At this moment when it runs I get the table that I'm wanting, though I still need to format it. But the iframe isn't even showing up. A previous solution I tried would pull up the iframe but inside would be an error where I was basically passing the sql query to the iframe.

<?php
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "purpletrainer";

// Create connection
$conn = new mysqli($servername, $username, $password);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//echo "Connected successfully";

$trainingid = $_POST["trainingid"];
$sql = "SELECT * FROM purpletrainer.trainingcontent WHERE trainingid = '$trainingid';";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table><tr><th>Training ID</th><th>Title</th><th>Training URL</th><th>Training Quiz URL</th></tr>";
// output data of each row
while ($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["trainingid"] . "</td><td>" . $row["trainingtitle"] . "</td><td>" . $row["trainingurl"] . "</td><td>" . $row["trainingquizurl"] . "</td></tr>";
}
echo "</table>";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}


$conn->close();
?>
<?php
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "purpletrainer";

// Create connection
$conn = new mysqli($servername, $username, $password);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$videosql = "SELECT trainingurl FROM purpletrainer.trainingcontent WHERE trainingid = '$trainingid';";
$videoresult = $conn->query($videosql);
if ($videoresult->num_rows > 0) {
while ($videourl = $videoresult->fetch_assoc()) {

}
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>

<div class="col-md-6">
<iframe width="420" height="315" src= '<?php echo htmlspecialchars($videourl); ?>' frameborder="0" allowfullscreen></iframe>
</div>

Answer

It's common practice not to use the variable $videourl outside of the while loop. (Put your iframe within the while loop). sidenote: fetch_assoc() will create an array. It should be $videourl['trainingurl'];

Here's an example:

<?php

if ($videoresult->num_rows > 0)
{
  while($videourl = $videoresult->fetch_assoc()){
?>
  <div class="col-md-6">
    <iframe width="420" height="315" src= '<?= $videourl['trainingurl']; ?>' frameborder="0" allowfullscreen></iframe>
  </div>
<?php
    }
  }
  else {
    echo "Error: " . $sql . "<br>" . $conn->error;
  }
?>

edit for clarity and future readers Note that the <?= $variable ?> opening tag has only been properly supported since php 5.4+. When not using this version, maintain the usage of <?php echo $variable; ?>