我正在尝试从 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);
?>
最佳答案
您可以在循环中插入 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);
?>
关于php - 在 PHP 和 MySQL 中显示来自 URL 的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30157349/