PHP从数据库显示图像 [英] PHP display images from database

查看:87
本文介绍了PHP从数据库显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在尝试显示我正在为朋友建立的网站的数据库中的图像.他希望能够上传图片并将其显示在首页上.

So I'm trying to display images from a database for a website that I'm building for a friend. He wants to be able to upload an image, and have it display on the front page.

我上传的图片全部正常.将其另存为BLOB.

I have the image uploading all working. Saving it in the database as a BLOB.

我开始研究将图像显示在单独的文件中.我在测试网站上运行它,图像显示了他想要它们的方式.但是后来我将代码移到了实际的网站上,了解了它的实际需要,并且无法正常工作.

I began working on displaying the images on a separate file. I got it working on my test website, images displayed how he wanted them. But then I moved the code over to the actual website, got it to how it needed to be and it didn't work.

我最终在测试网站上再次尝试,一切正常.

I ended up trying it again on my test website, and it all worked fine.

对于某些人来说,它不能在实际的网站上运行,但是在我的测试网站上却可以正常工作.

For some, it isn't working on the actual website, but it's working fine on my test website.

实际网站会获取我的测试网站所提供的更多信息.

The actual website grabs more information the my test website does.

这是我在测试网站上使用的代码.

Here is the code the I've used on my test website.

image.php

image.php

$query = mysql_query("SELECT * FROM images LIMIT 1");
while($row = mysql_fetch_assoc($query)){

    $image_id = $row['id'];
    echo "<img src=showimage.php?id=".$image_id.">";
}

这是我从数据库中抓取图像并显示它的方式.

Here is how I'm grabbing the image from the database and displaying it.

showimage.php

showimage.php

include 'inc/db.php';

$id = addslashes($_REQUEST['id']);

$image = mysql_query("SELECT image FROM sites WHERE id = '$id'");
$image = mysql_fetch_assoc($image);
$image = $image['image'];

header("Content-type: image/png");

echo $image;

使用测试网站上的代码可以正常工作,在那里没有问题.

Using that code on the test website works fine, there's no problem there.

这是我用于实际网站的代码.

Here is my code for the actual website.

sites.php

sites.php

$select = mysql_query("SELECT * FROM sites");
            while($row = mysql_fetch_assoc($select)){

                $image_id = $row['id'];

                echo '
                    <tr class="bottom"><td>'.$row['host'].'</td>
                    <td>'.$row['currency'].''.$row['price'].' every '.$row['payment'].'</td>
                    <td>'.$row['domain'].'</td>
                    <td>'.$row['paid_currency'].''.$row['paid_domain'].'</td>
                    <td>'.$row['features'].'</td>
                    <td>
                        <span title="Edit '.$row['id'].'"><a href="edit.php?id='.$row['id'].'"><img src="images/edit.png" alt="Edit"></a></span>
                        <span title="Delete '.$row['id'].'"><a href="inc/delete.php?id='.$row['id'].'"><img src="images/delete.png" alt="Delete"></a></span>
                    </td>
                    <td><img src=showimage.php?id='.$image_id.'></td></tr>
                    ';
            }

使用showimage.php文件从数据库中获取图像,但根本不起作用.

That using the showimage.php file to grab the image from the database, but isn't working at all.

推荐答案

如果您打算使用数据库,请使用

If you're set on using the database, look into using the imagecreatefrompng() function, depending on how the img was put into the DB.

此外,请尝试执行$id = (int)$_GET['id'];,然后检查是否为$id > 0,而不是addslashes().最后,在将图像存储在文件系统而不是BLOB上时+1.

Also, instead of addslashes(), try doing $id = (int)$_GET['id']; then checking if $id > 0. Finally, +1 on storing images on the filesystem and not as a BLOB.

[并在此处插入mysql_query-is-prepeded-use-the-PDO扩展讲座]

[And insert mysql_query-is-deprecated-use-the-PDO-extension lecture here]

这篇关于PHP从数据库显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆