简体   繁体   English

在PHP和MySQL中显示URL中的img

[英]Display img from URL in PHP & MySQL

I'm trying to grab an img URL from a mysql database and display the image itself, not the URL link. 我正在尝试从mysql数据库中获取img URL并显示图像本身,而不是URL链接。

Is there a way to concatenate in some HTML into the php script I'm running below? 有没有办法将一些HTML连接到我在下面运行的php脚本中?

Right now it displays the image URL just fine but would like it to display the actual image it's calling. 现在,它可以很好地显示图像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);
            ?>

You could insert the html img tag inside your loop. 您可以在循环中插入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);
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM