简体   繁体   English

PHP从数据库显示图像

[英]PHP display images from database

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. 他希望能够上传图片,并将其显示在首页上。

I have the image uploading all working. 我有图像上传所有工作。 Saving it in the database as a BLOB. 将其另存为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>
                    ';
            }

That using the showimage.php file to grab the image from the database, but isn't working at all. 使用showimage.php文件从数据库中抓取图像,但是根本不起作用。

If you're set on using the database, look into using the imagecreatefrompng() function, depending on how the img was put into the DB. 如果您准备使用数据库,请根据使用img放入数据库的方式来研究使用imagecreatefrompng()函数。

Also, instead of addslashes(), try doing $id = (int)$_GET['id']; 另外,请尝试执行$id = (int)$_GET['id'];而不是addslashes() $id = (int)$_GET['id']; then checking if $id > 0 . 然后检查$id > 0 Finally, +1 on storing images on the filesystem and not as a BLOB. 最后,在将图像存储在文件系统而不是BLOB上时+1。

[And insert mysql_query-is-deprecated-use-the-PDO-extension lecture here] [并在此处插入mysql_query-pre-deprecated-use-the-PDO扩展讲座]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM