我试图用PHP回应一些amp-html代码
<?php
echo "<section>";
echo "<h3>$title[$i]</h3>";
echo "<amp-video controls width="560" height="315" layout="responsive">";
echo "<source src="https: //example.com/videos/";
echo $filename[$i];
echo "_HD.mp4 type="video / mp4 / > ";
echo " < sourcesrc = "https://example.com/videos/";
echo $filename[$i];
echo '"__HD.webm" type="video/webm"/>';
echo "<div fallback><p>This browser does not support the video element.</p></div>";
echo "</amp-video>";
echo "</section>";
?>
我一直无法找到语法错误,我错过了什么?有没有比使用echo更简单的方法。
更新:我已经根据建议编辑了代码,但它仍然抛出一个空白页面:这是完整的代码:
<?php
// Create connection
$con=mysqli_connect("localhost","xx","xxxx","xxxx");
// Check connection
if (mysqli_connect_errno($con)) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
//Get number of rows
$sql="SELECT id,title,filename FROM videos";
$result=mysqli_query($con, $sql);
$i=1;
while($row=mysqli_fetch_assoc($result))
{
$id[$i] = $row['id'];
$title[$i] = $row['title'];
$filename[$i] = $row['filename'];
$i++;
}
// Loop through the results from the database
for ($i = 1; $i <=count($id); $i++)
{
?>
<section>
<h3><?php echo $title[$i];?></h3>
<amp-video controls width="560" height="315" layout="responsive">
<source src="https: //example.com/videos/<?php echo $filename[$i];?>_HD.mp4" type="video/mp4" />
<source src="https://example.com/videos/<?php echo $filename[$i];?>__HD.webm" type="video/webm" />
<div fallback><p>This browser does not support the video element.</p></div>
</amp-video>
</section>
}
</amp-accordion>
由于代码中的实际PHP输出很少,因此只需退出PHP即可输出HTML,这样可能更容易,更易读。
// Create connection
$con=mysqli_connect("localhost","xx","xxxx","xxxx");
// Check connection
if (mysqli_connect_errno($con)) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
//Get number of rows
$sql="SELECT id,title,filename FROM videos";
$result=mysqli_query($con, $sql);
while($row=mysqli_fetch_assoc($result)) {
?>
<section>
<h3><?php echo $row['title']; ?></h3>
<amp-video controls width="560" height="315" layout="responsive">
<source src="https: //example.com/videos/<?php echo $row['filename']; ?>_HD.mp4" type="video/mp4" />
<source src="https://example.com/videos/<?php echo $row['filename']; ?>__HD.webm" type="video/webm" />
<div fallback><p>This browser does not support the video element.</p></div>
</amp-video>
</section>
<?php
} // end of while
?>
</amp-accordion>
<?php
问题确实是使用您的语法。你应该尝试做的是当你使用html的回声时使用单引号,以及你在html中使用双引号添加的任何其他内容。
<?php
echo '<section>';
echo '<h3>$title[$i]</h3>';
echo '<amp-video controls width="560" height="315" layout="responsive">';
echo '<source src="https://example.com/videos/'.$filename[$i].'_HD.mp4" type="video / mp4 / >"';
echo '<source src="https://example.com/videos/'.$filename[$i].'__HD.webm" type="video/webm"/>"';
echo '<div fallback><p>This browser does not support the video element.</p></div>';
echo '</amp-video>';
echo '</section>';
?>
通过这种方式,可以更容易地发现语法错误,因为页面将显示必须显示的内容或显示代码。