我们可以在html属性中使用php标签吗?

时间:2015-03-18 12:40:10

标签: php html mysql

我已将图像路径存储在mysql表中。对于以下代码,图像无法加载。

<?php 
        mysql_connect('localhost','root','123');
        mysql_select_db('ratesl');
        $result = mysql_query("select name,rating_count,image,average from infos join ratings on infos.id = ratings.p_id order by average DESC ");
        while($row = mysql_fetch_object($result)){
            $img_path = "img/".$row->image; ?>
            <tr>
                <td style="padding:15px"> <img src=<?php $img_path; ?>/> </td>
                <td style="padding:15px"> <?php echo $row->name; ?></td>
                 <td style="padding:15px"> <?php echo $row->rating_count; ?></td>
                <td style="padding:15px"> <?php echo $row->average; ?></td>
            </tr>
       <?php } ?>     

althogh $ img_path包含数据库表中的值,img的src值保留为空。这是什么问题?

4 个答案:

答案 0 :(得分:1)

<img src="<?php echo $img_path?>" />

(别忘了引号:))

答案 1 :(得分:0)

忘记回显$ img_path

<?php 
            mysql_connect('localhost','root','123');
            mysql_select_db('ratesl');
            $result = mysql_query("select name,rating_count,image,average from infos join ratings on infos.id = ratings.p_id order by average DESC ");
            while($row = mysql_fetch_object($result)){
                $img_path = "img/".$row->image; ?>
                <tr>
                    <td style="padding:15px"> <img src=<?php echo $img_path; ?>/> </td>
                    <td style="padding:15px"> <?php echo $row->name; ?></td>
                     <td style="padding:15px"> <?php echo $row->rating_count; ?></td>
                    <td style="padding:15px"> <?php echo $row->average; ?></td>
                </tr>
           <?php } ?>   

答案 2 :(得分:0)

<img src=<?php echo $img_path; ?>

答案 3 :(得分:0)

您的代码缺少报价并回显..

<img src=<?php $img_path; ?>/>
        ^^^   ^^^          ^^^

你需要报价

<img src="<?php echo $img_path; ?>"/>
        ^^^                      ^^^