endorush85 Posted July 12, 2022 Share Posted July 12, 2022 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. Quote Link to comment https://forums.phpfreaks.com/topic/315030-trying-to-pull-url-from-db-and-use-in/ Share on other sites More sharing options...
maxxd Posted July 12, 2022 Share Posted July 12, 2022 <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. Quote Link to comment https://forums.phpfreaks.com/topic/315030-trying-to-pull-url-from-db-and-use-in/#findComment-1598148 Share on other sites More sharing options...
endorush85 Posted July 14, 2022 Author Share Posted July 14, 2022 (edited) 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 July 14, 2022 by endorush85 Quote Link to comment https://forums.phpfreaks.com/topic/315030-trying-to-pull-url-from-db-and-use-in/#findComment-1598202 Share on other sites More sharing options...
Phi11W Posted July 14, 2022 Share Posted July 14, 2022 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. Quote Link to comment https://forums.phpfreaks.com/topic/315030-trying-to-pull-url-from-db-and-use-in/#findComment-1598209 Share on other sites More sharing options...
endorush85 Posted July 14, 2022 Author Share Posted July 14, 2022 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 Quote Link to comment https://forums.phpfreaks.com/topic/315030-trying-to-pull-url-from-db-and-use-in/#findComment-1598210 Share on other sites More sharing options...
Barand Posted July 14, 2022 Share Posted July 14, 2022 You can't just switch to HTML in the middle of PHP code. Use the code @Phi11W gave you Quote Link to comment https://forums.phpfreaks.com/topic/315030-trying-to-pull-url-from-db-and-use-in/#findComment-1598211 Share on other sites More sharing options...
endorush85 Posted July 14, 2022 Author Share Posted July 14, 2022 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! Quote Link to comment https://forums.phpfreaks.com/topic/315030-trying-to-pull-url-from-db-and-use-in/#findComment-1598212 Share on other sites More sharing options...
endorush85 Posted July 14, 2022 Author Share Posted July 14, 2022 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 Quote Link to comment https://forums.phpfreaks.com/topic/315030-trying-to-pull-url-from-db-and-use-in/#findComment-1598217 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.