【问题标题】:Display img from URL in PHP & MySQL在 PHP & MySQL 中显示来自 URL 的 img
【发布时间】:2015-05-10 22:35:37
【问题描述】:

我正在尝试从 mysql 数据库中获取 img URL 并显示图像本身,而不是 URL 链接。

有没有办法将一些 HTML 连接到我在下面运行的 php 脚本中?

现在它可以正常显示图像 URL,但希望它显示它正在调用的实际图像。

<?php
            $db = new mysqli('localhost', 'root', 'root', 'testdb');

            if($db->connect_errno > 0){
                die('Unable to connect to database [' . $db->connect_error . ']');
            }

            $sql = <<<SQL
                SELECT *
                FROM `site_img`
            SQL;

            if(!$result = $db->query($sql)){
                die('There was an error running the query [' . $db->error . ']');
            }
            while($row = $result->fetch_assoc()){
                echo $row['img_id'] . ' ' . $row['img_url'] . '<br />';
            }
            echo 'Total results: ' . $result->num_rows;

            // Free result set
            mysqli_free_result($result);

            mysqli_close($db);
            ?>

【问题讨论】:

  • echo '&lt;img src="'.$row['img_url'].'"&gt;';
  • 完美,完美运行

标签: php html mysql


【解决方案1】:

您可以在循环中插入 html img 标签。

<?php
            $db = new mysqli('localhost', 'root', 'root', 'testdb');

            if($db->connect_errno > 0){
                die('Unable to connect to database [' . $db->connect_error . ']');
            }

            $sql = <<<SQL
                SELECT *
                FROM `site_img`
            SQL;

            if(!$result = $db->query($sql)){
                die('There was an error running the query [' . $db->error . ']');
            }
            while($row = $result->fetch_assoc()){
?>
            <img name="<?php echo $row['img_id'] ?>" src="<?php $row['img_url'] ?>" /> <br />
<?php
            }
            echo 'Total results: ' . $result->num_rows;

            // Free result set
            mysqli_free_result($result);

            mysqli_close($db);
?>

【讨论】:

    猜你喜欢
    • 2021-07-02
    • 2021-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-12
    • 2019-11-08
    • 2016-07-18
    相关资源
    最近更新 更多