我试图从mysql数据库中获取img URL并显示图像本身,而不是URL链接。
有没有办法将某些HTML连接到我在下面运行的php脚本中?
现在它显示图片网址很好但希望它显示它正在调用的实际图片。
<?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);
?>
答案 0 :(得分:0)
您可以在循环中插入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);
?>