dqh19413 2013-01-03 02:48
浏览 78
已采纳

如何从PHPMyAdmin获取图像并加载到网页中

I am doing PHP for the first time and research about this but not joy. Basically, i am trying to get the images from my PHPMyAdmin database and upload it to a webpage that i have created. All the heading and texts are being displayed as intended apart from the image.

Code in which i am getting the images from database

    <?php
        include 'config.php';
        include 'opendb.php';
        $getdestinationdetails = mysql_query("SELECT *
        FROM tbldestinations
        WHERE destinationid = $_GET[destinationid]");
        while($row = mysql_fetch_array($getdestinationdetails))
        {
        echo "<h1>Visit " . $row['destinationname'] . "</h1>".
        "<imgsrc = 'images" . $row['destinationimage'] . "' />"  .
        "<br />
        <br />" .
        $row['destionationdesc'] .
        "<br />
        <br />
        Our holidays include:
        <ul>
            <li>Luxury coach travel from Shipley Town Centre</li>
            <li>Bed and Breakfast at a top hotel</li>
            <li>Free tea and coffee on board your coach</li>
            <li>The services of an experienced courier</li>
            <li>Free complimentary glass of champagne upon arrival at your hotel</li>
        </ul>
        <center><h2>Don't delay, book today!</h2></center>";
        }
        include 'closedb.php';
?>

I tried all the experiments but all in vain. any tips or suggestions would be really appreciated. Thanks in advance

  • 写回答

1条回答 默认 最新

  • dongyan5641 2013-01-03 02:57
    关注

    If the actual image is stored in the database, you have two options:

    1. Make another script that is designed to just load images from the database. In this script, you will set the header('Content-Type: image/jpeg') (or whatever the type of image that you will display is. Then, you will output the content. This option might not be as preferred, as it takes an extra call to the database (http://stackoverflow.com/questions/5525830/displaying-an-image-stored-in-a-mysql-blob)
    2. A little more risky, for browser compatibility is to base64 encode it and output it directly into the image tag. (http://stackoverflow.com/questions/1207190/embedding-base64-images)

    Either option, you will need to still have an <img /> tag on your main page. Option 1 requires a src attribute pointing to the script, including whatever identifiers needed to look up the image in the database (but being very careful about XSS). Option 2 outputs it directly into the image tag, so that is preferable in some respects as well

    If it is just a link to an image:

    Your above code should work fairly fine, except with this modification:

    echo "<h1>Visit " . $row['destinationname'] . "</h1>".
         "<img src=\"images/" . $row['destinationimage'] . "\" />"  .
    ...
    

    Edit:

    What I would do, which would be the easiest is this: in your php file, output your image (assuming it is a jpg), like this:

    ...
    echo '<img src="data:image/jpeg;base64,' . base64_encode($row['destinationimage']) . '" />';
    ...
    

    The above will actually put the image into the image tag itself, instead of downloading across the internet. I think the hangup here is the fact that you are seeing the image in PHPmyAdmin. When you see the image there, there really is no way to get it out of PHPmyAdmin. I haven't tried it myself, but I would assume that they are doing something similar to what I show you above - just displaying the image in the HTML tag.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。