How to insert a YouTube embed using PHP into MySQL and retrieve it again

1.1k views Asked by At

I have the following: Youtube embed code:

<iframe class="embed-responsive-item" src="https://www.youtube.com/embed/nqNp9TgnXHk" allowfullscreen=""></iframe>

I have a table where I store the youtube "nqNp9TgnXHk" part of the embed code.

  • id - type is integer,
  • tube_code - type is varchar ("where I store the embed nqNp9TgnXHk")

Using PHP how do I echo the string:

<iframe class="embed-responsive-item" src="https://www.youtube.com/embed/.$tube_code. allowfullscreen=""></iframe>

...so that I can insert and retrieve it from the ḾySQL table?

1

There are 1 answers

2
Mario A On BEST ANSWER

Try something like this:

<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");
$result = mysqli_query($con,"SELECT * FROM Yourtable Where id='xyz'");
$row = mysqli_fetch_object();

?>
<iframe class="embed-responsive-item" src="https://www.youtube.com/embed/<?=$row->tube_code?>" allowfullscreen=""></iframe>

... where xyz is the id of the stored youtube id.