PDA

View Full Version : Retrieving Image from db


ptmuldoon
02-13-2008, 07:15 PM
I'm trying to retrieve an image (users avatar) from a db, but it only seems to return a blank image. From all my searches, I think I'm on the right track, but its not quite there yet.

I have two files, an test_image file, and an avatar file.

test_image File is just to test and pass an id to the avatar file
<img src="avatar.php?image_id=74"></img>

and the avatar file is

// get the image from the db
$sql = "SELECT avatar, image_type FROM users WHERE id='".$_GET['image_id']."'";
// the result of the query
$result = mysql_query($sql);
$row = mysql_fetch_array($result);

header("Content-type: ".$row['image_type']);
echo $row['avatar']

_Aerospace_Eng_
02-13-2008, 07:37 PM
// get the image from the db
$id = intval($_GET['image_id']); // convert string to int
$sql = "SELECT avatar, image_type FROM users WHERE id = $id";
// the result of the query
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_array($result);

header("Content-type: ".$row['image_type']);
echo $row['avatar'];

ptmuldoon
02-13-2008, 08:16 PM
Thanks

For some strange reason, It works when I upload it to my live site. But in local testing with a WAMP installation, I still get a blank page, with no image. Not sure why it doesn't want to work locally. I can only quess possibly an issue with a php or mysql extension not be configured or turned on?