Whether it is website design, office system, or shopping mall, pictures need to be displayed on the web page. The pictures are extracted from the database. Next, let’s take a look at how to save and display pictures through the picture path:
First, save the path of the image in the table. Note: the path is to be found from the WWW directory instead of from the disk root , / represents the WWW directory : (Recommended learning: PHP video tutorial)
The img in the table is the path to the stored image, which is the path under the WWW directory. Since the path is stored in the table , then we can get it through PHP linking to the database:
<?php require "DBDA.class.php";//调用封装类 $db = new DBDA(); $sql = "select * from book"; $arr= $db->query($sql); foreach($arr as $v) // 遍历出各个列的内容 { //图片用img标签获取路径,因为遍历出的img是$v[5],所以路径就是$v[5] echo "<div id='hang'><a href='gouwu.php?bkid={$v[0]}'><img style="max-width:90%"How to display pictures in database in php" ></a> <div class='jg'><a href='gouwu.php?bkid={$v[0]}'>{$v[1]}</a></div> <div class='jg' style='color:red'>{$v[3]}元</div> </div> "; } ?>
In this way, we can get the picture through the path, which is very convenient and efficient:
The above is the detailed content of How to display pictures in database in php. For more information, please follow other related articles on the PHP Chinese website!