mySQL数据库和PHP中的URL字符串


URL string in mySQL database and PHP

我遇到了存储在数据库中的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);

给我带来麻烦的是$rotasimg变量。在数据库中,我手动输入了rotator.imageURL的值,如下所示:"images/rotor/name.jpg",但当我从数据库中查询它们时,请将它们添加到$rotatorsDisplay变量中,然后用以下代码输出:

    <?php echo $rotatorsDisplay; ?> 

我在结果页面中得到的是:

    <a href="doctor.php"><img src=""></a>

我确信这不起作用的原因是URL中的斜杠。我也很确定我需要以某种方式编码/转换HTML,从我输入数据库的代码中去除特殊字符,然后在输出时解码/转换回HTML,但由于我使用phpMyAdmin手动将这些信息输入数据库,我还没有想出如何做到这一点。

感谢您的帮助!

看起来您使用了错误的索引键。

$rotatorsimg = $row["imgURL"];

应该是

$rotatorsimg = $row["imageURL"];

因为这是您从数据库中查询的内容。