Jump to content

Recommended Posts

I can connect to the database, and when I run a simple query, it prints the url on the web page. But what I really want to do is use that url (youtube) and use it as a variable to put inside an <IFRAME> to embed on my site.

Heres the code I'm working with:

<!DOCTYPE html>
	<html>
		<head>
			<meta name="viewport" content="width=device-width, initial-scale=1.0">
			<link rel="stylesheet" type="text/css" href="style.css">


		</head>


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

			// Create connection
			$conn = new mysqli($servername, $username, $password, $dbname);
			// Check connection
			if ($conn->connect_error) {
			  die("Connection failed: " . $conn->connect_error);
			}else {
				echo "Connection Established.";
			}

						$sql = "SELECT url FROM songs";
						$result = $conn->query($sql);

						if ($result->num_rows > 0) {
						  // output data of each row
						  while($row = $result->fetch_assoc()) {
						    echo "id: " . $row["url"] ;
						  }
						} else {
						  echo "0 results";
						}					
					?>

 this part right here:

while($row = $result->fetch_assoc()) {
	echo  . $row["url"] ;
}

that prints the youtube link.....

 

How could I make this link into a variable to supply as a parameter for this:

 <iframe width="420" height="315"
src="https://www.youtube.com/embed/tgbNymZ7vqY"> // variable goes here
</iframe> 

anybody that could help me make this work I'd greatly appreciate it.

Link to comment
https://forums.phpfreaks.com/topic/315030-trying-to-pull-url-from-db-and-use-in/
Share on other sites

<iframe width="420" height="315" src="<?php echo $row['url']; ?>"></iframe>

This is a basic example - there are other, better ways to do this. For instance, the short echo tag is easier to read. The best idea is to use a template language like Twig, but that is a far more advanced operation. You'll also want to look into escaping your output as well.

Thanks for the reply.

I tried that code but it still doesn't work.  It creates the IFRAME, but its just a black box, no content. When I look at the page source in the browser it says

 

<iframe width="420" height="315" src=""></iframe>

it wont transfer the variable.  it's the exact problem I've been having and don't know why this does't work

 

it seems so simple, pull a variable from the db, and plug it into the IFRAME src attribute. It's got to be incorrect syntax or maybe scope problem, I'm still working on it, any tips are appreciated.

Edited by endorush85
3 hours ago, endorush85 said:

It creates the IFRAME ...

What creates the IFRAME?  Show us your modified code. 

I'd hope it looks something like this: 

while( $row = $result->fetch_assoc() ) {
	printf( '<iframe width="420" height="315" src="%s"></iframe>', $row['url'] );
}

Regards, 
   Phill  W. 

heres the code

<!DOCTYPE html>
	<html>
		<head>
			<meta name="viewport" content="width=device-width, initial-scale=1.0">
			<link rel="stylesheet" type="text/css" href="style.css">


		</head>


		<?php
			$servername = "";
			$username = "";
			$password = "";
			$dbname = "";

			// Create connection
			$conn = new mysqli($servername, $username, $password, $dbname);
			// Check connection
			if ($conn->connect_error) {
			  die("Connection failed: " . $conn->connect_error);
			}else {
				echo "Connection Established.";
			}

			$sql = "SELECT url FROM songs";
			$result = $conn->query($sql);

			if ($result->num_rows > 0) {
			  
			  while( $row = $result->fetch_assoc() ) {
				<iframe width="420" height="315" src=$row['url']></iframe>;
			}		

			} else {
			  echo "0 results";
			}		
		?>

all I get when I try to open in the browser is a blank screen

okay I sort of got it working, thanks to all yall's help...

it's finally feeding the variable into the attribute properly, i've confirmed that, however now I have another problem which is probaly outside the scope of php/html etc

I get the frame, and inside of it it looks like this:

------------------------------------------------

Firefox Can’t Open This Page

To protect your security, www.youtube.com
will not allow Firefox to display the page if
another site has embedded it. To see this page,
you need to open it in a new window.

Learn more…

-----------------------------------------

 

when I click on that frame, it will load in a new window on the youtube site, but it won't embed.  They must have changed something recently because I used to be able to embed videos like that. I even tried again without the variable, no php, just plain text in the src attribute and it gives the same error... some youtube permissions problem.

 

Well at least the problem I was concerned about has been solved.  Thanks again for all your help.  I'm pretty good with C# C++ But PHP has been kind of tricky for me so far.   Thanks!

aha!  I figured it out

I had the link wrong in the database. i was using

https://www.youtube.com/watch?v=_YZuCdS5_t0

when I should have been using:

https://www.youtube.com/embed/_YZuCdS5_t0

 

I was using the "watch" link like the actual link, I didn't realize you had to swap "watch?v=" with "embed/"

problem solved, frikken beautiful, thanks again!

hopefully if someone else has this same problem they will find this thread and it will help them

Thanks

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.