Jump to content

Displaying images from database


benji87

Recommended Posts

Hi all ive got this code to display uploaded images from a database but at the moment it will only display one image.

Here is my code:

[code]
$gotten = @mysql_query("SELECT * FROM pix ORDER BY pid DESC");
if ($row = @mysql_fetch_assoc($gotten)) {
        $title = htmlspecialchars($row[title]);
        $bytes = $row[imgdata];
} else {
        $errmsg = "MySql Error!";
}


if ($_REQUEST[gim] == 1) {
        header("Content-type: image/jpeg");
        print $bytes;
        exit ();
        }
?>

<img src=?gim=1 width=80>[/code]

Now i know its controlled by the 'gim' variable but im not sure on how to modify this to made it display all the images contained in the database. Any help would be great! Thanks
Link to comment
Share on other sites

Pls try like this:
if ($_REQUEST[gim] == 1) {

$gotten = @mysql_query("SELECT * FROM pix ORDER BY pid DESC");
while($row = @mysql_fetch_array($gotten)) {
        $title = htmlspecialchars($row[title]);
        $bytes = $row[imgdata];
        header("Content-type: image/jpeg");
        print $bytes;
        } else {
        $errmsg = "MySql Error!";
}

}



Sorry if am wrong some where. Just felt like giviving a hint.
Link to comment
Share on other sites

You can't do that... At least not in the way your thinking...

Example to show all images...

DisplayImage.php
[code]
<?php

$query = sprintf("SELECT * FROM `pix` WHERE `pid`='%d'", $_GET["gim"]); // mysql injection possible, quote $_GET["gim"] (research SQL injection)
$gotten = mysql_query($query) or die(mysql_error());
if($row = mysql_fetch_assoc($gotten))
{
$title = htmlspecialchars($row["title"]);
$bytes = $row["imgdata"];
}else{
        $errmsg = "MySql Error!";
}

header("Content-type: image/jpeg");
print $bytes;
exit();

?>
[/code]

Then, to show all the images...

ShowAll.php
[code]
<?php
$query = "SELECT * FROM `pix` ORDER BY `pid` DESC";
$gotten = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_assoc($gotten))
{
  echo '<img src="DisplayImage.php?gim=' . $row["pid"] . '" alt="' . htmlentities($row["title"]) . '">';
}
?>
[/code]

Hth, Note haven't tested or checked for errors :)

Link to comment
Share on other sites

Thanks mate thats great! Ive got two error's. On displayimage.php it doesnt show an image just a broken image. Also in showall.php it says unexpected t_string on line 12 but ive tried putting bits here and there but it still comes up with the same error!  :-[ sorry to be a pain!
Link to comment
Share on other sites

Thanks for the link but ive already achieved what the tutorial explains. I know how to upload an image and display it. The only trouble is i want to display all the images in the database which is achievable in the code heckenschutze gave me but i cant get it to work  :-\
Link to comment
Share on other sites

Ok im getting somewhere now the code that heckenschutze has given me works apart from it wont show the images or the title. Although it does draw the title as an alt attribute. Someone please help i know im almost there with this!

here is the page: [url=http://www.alns.co.uk/ssrfc/showall.php]http://www.alns.co.uk/ssrfc/showall.php[/url]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.