-1

This is a code that connects to the data base and takes the first ending entry to assign it to $fname variable, i am not sure why the variable does not seem to work.

$fname = 1I21Z5wNQ48 .

If i type it in directly like this //www.youtube.com/embed/1I21Z5wNQ48 ,it works but if i type the variable in (//www.youtube.com/embed/$fname), it does not. Gives me a black screen saying "A Error Occurred" (In context to embed link.) Thank you for reading and answering my question

<?php
//Connect Script
$output = "";
$cxn = mysqli_connect("host","Username","Password", "DB");
mysqli_connect("host", "Username", "Password", "DB") or die (mysqli_error($cxn));

//Collect -
$query = mysqli_query($cxn, "SELECT * FROM `links` ORDER BY `id` DESC") or die (mysqli_error($cxn));

$count = mysqli_num_rows($query);
if ($count == 0) {
    $output = " No results found ! ";
} else {
    while ($row = mysqli_fetch_array($query)) {
        $fname = $row['taglink']; // Is currently only outputting the first enntry: 1I21Z5wNQ48
        $id = $row['id'];
    }
  }
?>

<!DOCTYPE html>
<html lang="en">

<html>
<head>

<meta http-equiv="content-type" content="text/html; charset=utf-8" />
</head>
<body>

<iframe width="560" height="315" src="//www.youtube.com/embed/$fname" frameborder="0"     allowfullscreen></iframe>

</body>
</html>
John Conde
  • 217,595
  • 99
  • 455
  • 496
CharlieC
  • 123
  • 1
  • 8

1 Answers1

1

You don't have your variable wrapped in PHP tags so it is ignored by the PHP processor.

<iframe width="560" height="315" src="//www.youtube.com/embed/<?= $fname ?>" frameborder="0"     allowfullscreen></iframe>

I used shorthand tags here. You can, of course, use long form a well:

<iframe width="560" height="315" src="//www.youtube.com/embed/<?php echo $fname ?>" frameborder="0"     allowfullscreen></iframe>
John Conde
  • 217,595
  • 99
  • 455
  • 496
  • Ah, thanks for your help, working now, i also realised that i had to declare it first, look at the top $output. I should have done the same for $fname else it would give me null or not work. – CharlieC Nov 26 '14 at 03:45
  • `http:` not needed? Fancy trick no one told me about? lol I guess it needs to be on the server in order for it to work. – Funk Forty Niner Nov 26 '14 at 03:45
  • [It actually is optional.](http://stackoverflow.com/a/8951636/250259). Useful for avoiding those pesky https errors. :) – John Conde Nov 26 '14 at 03:48
  • @JohnConde Wouldn't work if ran locally though. I remember using similar syntax with jQuery files, and only ran if I viewed it via the WWW. – Funk Forty Niner Nov 26 '14 at 03:50
  • @JohnConde Well, to be more specific. Not an `.php` file but an `.html` file where the script would point to a starting `//jquery.com/script.js` for example, running it in my browser directly from a file in my PC would not work (via file://file.html). If I uploaded it to my server and viewed it via WWW, then it would work. – Funk Forty Niner Nov 26 '14 at 03:53
  • @Fred-ii- I think the Internet was rooting against you that day! When I get the chance I'll have to try it on my home setup. You've stoked my curiosity. – John Conde Nov 26 '14 at 03:54
  • @JohnConde It has rooted me on quite a few occasions actually. Check it out and let me know. – Funk Forty Niner Nov 26 '14 at 03:55
  • Actually, you can try it out yourself with the OP's iframe code, but run it as just `.html` with the YouTube address. I.e.: `` then try `` - I tested it now, same thing; doesn't run without `http:` – Funk Forty Niner Nov 26 '14 at 03:58
  • I work from home tomorrow so I'll give this a try when I am in front of my pc and not laying in bed postponing sleep. lol – John Conde Nov 26 '14 at 03:59