如何使用PHP显示存储在mysql数据库中的图像

问题描述:

I have been trying to display image from database using php but i am only getting download prompt whenever i use or click the link. what i want to do is, i want to display it on the web browser. And want to use it in tags.

my code is:

require_once('dbconfig.php');
        $cont=mysql_connect($dbhost,$dbuser,$dbpass) or die("Error Connecting the Database");
        $seldatabase=mysql_select_db($dbselect,$cont);
        $insert = "SELECT `p_mime`, `p_name`, `p_size`, `p_data` FROM $dbtbl_reg_details WHERE `id` = $id";
        $query = mysql_query($insert);
        if ($query)
        {
            if (mysql_num_rows($query)==1)
            {
                $row = mysql_fetch_assoc($query);
                header("Content-Type: image/png");
                header("Content-Length: ". $row['p_size']);
                header("Content-Disposition: inline; filename=". $row['p_name']);
                echo $row[p_data];
            }
        else
        {
            echo "image with id = ".$id." does not exist";
        }
    }
    else
    {
        echo "query failed, image with id = ".$id." does not exist";
    }

And when i use inline for Content-Disposition: then my web page returns a script error on the browser.

So, what should i do to display images on web pages while retrieving it from mysql database

我一直在尝试使用php从数据库显示图像,但我只是在使用或点击 链接。 我想要做的是,我想在网络浏览器上显示它。 并希望在标签中使用它。 p>

我的代码是: p>

  require_once('dbconfig.php'); 
 $ cont  = mysql_connect($ dbhost,$ dbuser,$ dbpass)或die(“连接数据库时出错”); 
 $ seldatabase = mysql_select_db($ dbselect,$ cont); 
 $ insert =“SELECT`p_mime`,`p_name  `,`p_size`,`p_data` FROM $ dbtbl_reg_details WHERE`id` = $ id“; 
 $ query = mysql_query($ insert); 
 if($ query)
 {
 if if(mysql_num_rows($ query)  )== 1)
 {
 $ row = mysql_fetch_assoc($ query); 
 header(“Content-Type:image / png”); 
 header(“Content-Length:”。$ row ['p_size  ']); 
 header(“Content-Disposition:inline; filename =”。$ row ['p_name']); 
 echo $ row [p_data]; 
} 
 else 
 {
 echo“  id =“。$ id。”的图像不存在“; 
} 
} 
 
 
 
 
 
 
 
 
 
 \ {
 
 
 
 
 
 
呼叫”查询失败,图片ID =“。$ id。”不存在“; \  n} 
  code>  pre> \  n 
 

当我使用内联进行内容处理:然后我的网页在浏览器上返回一个脚本错误。 p>

那么,我该怎么做才能在网上显示图像 从mysql数据库中检索它时的页面 p> div>

if (mysql_num_rows($query)==1)
{
    while( @ob_end_clean() );

    header("Content-type:  image/png");
    header("Content-Length: ". $row['p_size']);
    header("Content-Disposition: attachment; filename=\"{$row['p_name']}\"");

    die($row[p_data]);
}

why u have echo hi; ?

its probably cause an error

and also add exit in the end (to sure you dont echo anything after)

echo $row[p_data];
exit;