简体   繁体   English

从MySQL加载图像的正确方法

[英]proper way to load images from mysql

将图像的img src =“” ....存储在数据库表列中是否可以接受,还是应该只存储位置,然后以某种方式通过javascript进行操作才能将图像加载到网页上?

you would only store the image path (and possibly also the alt text and title) in the db and then add it into the webpage using php when the page loads. 您只需将图像路径(以及可能的替代文本和标题)存储在数据库中,然后在页面加载时使用php将其添加到网页中。 Note that in the following I am echoing each attribute independently. 请注意,下面我将独立地回显每个属性。 You can also put the entire image line inside an echo statement and cut out a couple of echoes with just the php db values listed as the attribute values. 您也可以将整个图像行放在echo语句中,并仅将php db值列为属性值,以剪除几个echo。

//your mechanism of getting the data from the db

$imagePath=$Row["imagePath"]; //for example "images/common/logo.jpg";
$imageAlt=$Row["imageAlt"]; //for example "Our Company logo image";
$imageTitle=$Row["imageTitle"]; //for example "Our Company logo for 2016";

//image section of your HTML
<img src='<?php echo "$imagePath";?>' alt='<?php echo "$imageAlt";?>' title='<?php echo "$imageTitle";?>' height='50' width='50'/> 

this will render as 这将呈现为

 <img src='images/common/logo.jpg' alt='Our Company logo image' title='Our Company logo for 2016' height='50' width='50'/> 

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

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