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

标签 php mysql special-characters

我在数据库中存储的 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/

相关文章:

android - 特殊字符显示为问号哈希

php - 更改为 SSL 域会丢失子域多商店设置中的语言 (Magento)

php - Web 服务器上的哪个位置是存储包含简历文件的文件夹的最佳位置?内部网络根目录还是外部网络根目录?什么最安全?

php - 每次针对不同条件动态获取查询

swift - 将字符串中的重音字符替换为基本字符 - ą -> a , ć -> c

ios - 具有特殊字符的 Swift 正则表达式

php - Laravel maatwebsite excel包导入2000+行导致1390代码错误

php - UTF8 与数据库问题

php - 在 MYSQL 表中存储 Facebook 用户喜欢数组

mysql - 连接到远程 MySQL 主机解析为我的 IP 地址?