【问题标题】:Youtube Embed Error using variable使用变量的 Youtube 嵌入错误
【发布时间】:2015-01-24 07:07:58
【问题描述】:

这是一个连接到数据库并获取第一个结束条目以将其分配给$fname 变量的代码,我不确定为什么该变量似乎不起作用。

$fname = 1I21Z5wNQ48 .

如果我像 //www.youtube.com/embed/1I21Z5wNQ48 那样直接输入它,它可以工作,但如果我在 (//www.youtube.com/embed/$fname) 中输入变量,它不会。给我一个黑屏说“发生错误”(在嵌入链接的上下文中。)感谢您阅读并回答我的问题

<?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>

【问题讨论】:

    标签: php mysql variables youtube embed


    【解决方案1】:

    您的变量没有包含在 PHP 标记中,因此 PHP 处理器会忽略它。

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

    我在这里使用了速记标签。当然,您可以使用长形井:

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

    【讨论】:

    • 啊,谢谢你的帮助,现在工作,我也意识到我必须先声明它,看看顶部的 $output。我应该为 $fname 做同样的事情,否则它会给我 null 或不起作用。
    • http: 不需要?没有人告诉我的花哨的技巧?大声笑我想它需要在服务器上才能工作。
    • It actually is optional.。对于避免那些讨厌的 https 错误很有用。 :)
    • @JohnConde 如果在本地运行将无法工作。我记得在 jQuery 文件中使用过类似的语法,并且只有在我通过 WWW 查看时才会运行。
    • @JohnConde 好吧,更具体地说。不是.php 文件,而是.html 文件,其中脚本将指向起始//jquery.com/script.js,例如,直接从PC 中的文件在浏览器中运行它是行不通的(通过file://file.html )。如果我将它上传到我的服务器并通过 WWW 查看它,那么它会起作用。
    猜你喜欢
    • 1970-01-01
    • 2013-03-26
    • 2017-08-11
    • 1970-01-01
    • 2011-02-01
    • 2011-09-01
    • 2015-10-19
    • 2013-10-01
    • 2012-12-24
    相关资源
    最近更新 更多