我在数据库中存储的 URL 无法正确显示时遇到问题。这是我的代码:
$sqlCommand =
"SELECT rotator.title, rotator.imageURL, rotator.targetURL, rotator.caption FROM rotator
WHERE ( ( rotator.visible = '1' )
OR ( rotator.visible = '2' AND ( NOW() BETWEEN rotator.schedstart AND rotator.schedstop ) ) )
ORDER BY rotator.displayorder ASC";
$query = mysqli_query($myConnection,$sqlCommand) or die (mysqli_error($myConnection));
$rotatorsDisplay = '';
while ($row = mysqli_fetch_array($query)){
$rotatorstitle = $row["title"];
$rotatorsimg = $row["imgURL"];
$rotatorsURL = $row["targetURL"];
$rotatorscaption = $row["caption"];
$rotatorsDisplay .= '<li>
<a href="'. $rotatorsURL .'"><img src="'. $rotatorsimg .'"></a>
<div id="title">'. $rotatorstitle .'</div>
<div class="caption">'. $rotatorscaption .'</div>
</li>';
}
mysqli_free_result($query);
给我带来麻烦的是 $rotatorsimg 变量。在数据库中,我手动输入了 rotator.imageURL 的值,如下所示:'images/rotator/name.jpg' 但是当我从数据库中查询它们时,将它们添加到 $rotatorsDisplay 变量,然后使用此代码输出它们:
<?php echo $rotatorsDisplay; ?>
我在结果页面中得到的是这样的:
<a href="doctor.php"><img src=""></a>
我很确定这不起作用的原因是 URL 中的斜杠。我也很确定我需要以某种方式编码/转换 HTML 以从我输入数据库的代码中去除特殊字符,然后在我输出它时将其解码/转换回 HTML,但是因为我使用手动将此信息输入数据库phpMyAdmin,我还没有想出如何做到这一点。
感谢任何帮助!
最佳答案
您似乎使用了错误的索引键。
$rotatorsimg = $row["imgURL"];
应该是
$rotatorsimg = $row["imageURL"];
因为这就是您要从数据库中查询的内容。
关于php - mySQL 数据库和 PHP 中的 URL 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12433279/