get image from mysql database using php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hero789
Forum Newbie
Posts: 23
Joined: Sun Dec 20, 2009 5:10 pm

get image from mysql database using php

Post by hero789 »

Hi
I am trying to take an image that I have stored in a MySQL database and display it to screen. My PHP is as follows for:-
<?php
ob_start();

//include the database connection file
include ($_SERVER['DOCUMENT_ROOT'].'\project\website\connect\databaseconnect.php');


$query = "select title, first_name, last_name, house_number, street, town, county, postcode, contact_number, emailaddress, username, password, image_title, image_file, image_type, image_size, expertise, about from trainer";
$result = mysql_query($query);

while ($row = mysql_fetch_assoc($result))
{
$title = $row['title'];
$firstname= $row ['first_name'];
$lastname = $row ['last_name'];
$house_number = $row['house_number'];
$street = $row ['street'];
$town = $row ['town'];
$county = $row ['county'];
$postcode = $row ['postcode'];
$telephone = $row ['contact_number'];
$email = $row ['emailaddress'];
$username = $row ['username'];
$password = $row ['password'];
$image_title = $row['image_title'];
$image_file = $row['image_file'];
$image_type = $row['image_type'];
$image_size = $row['image_size'];
$expertise = $row['expertise'];
$about = $row['about'];



echo $title;
echo $firstname;
echo $lastname;
echo $house_number;
echo $street;
echo $town;
echo $county;
echo $postcode;
echo $telephone;
echo $email;
echo $username;
echo $password;
echo $expertise;
echo $about;
header("Content-type: $image_type");
echo $image_file;

}
?>
Any help would be wonderful. It should be working. All i get displayed to screen is a picture box with a red x in it nothing else.
hero789
Forum Newbie
Posts: 23
Joined: Sun Dec 20, 2009 5:10 pm

Re: get image from mysql database using php

Post by hero789 »

Problem solved. The echo [text] was interfering with the display of the message. I took out all the echo statements leading up to the header() statement and now I have display of the image. I can work with this. Thanks for anybody thinking of helping.
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Re: get image from mysql database using php

Post by daedalus__ »

you can't output anything to the browser before the headers, that's why its a header. dont' worry though a lot of people miss that when they are new.
http://us2.php.net/manual/en/function.header.php wrote: Remember that header() must be called before any actual output is sent
this whole deal is pretty unnecessary anyway though:

Code: Select all

 
$title = $row['title'];
$firstname= $row ['first_name'];
$lastname = $row ['last_name'];
$house_number = $row['house_number'];
$street = $row ['street'];
$town = $row ['town'];
$county = $row ['county'];
$postcode = $row ['postcode'];
$telephone = $row ['contact_number'];
$email = $row ['emailaddress'];
$username = $row ['username'];
$password = $row ['password'];
$image_title = $row['image_title'];
$image_file = $row['image_file'];
$image_type = $row['image_type'];
$image_size = $row['image_size'];
$expertise = $row['expertise'];
$about = $row['about'];
 
nga
Forum Commoner
Posts: 46
Joined: Mon Aug 17, 2009 3:05 am

Re: get image from mysql database using php

Post by nga »

why dont you just input the img path into the database and echo the path into <img src="path"/>?
hero789
Forum Newbie
Posts: 23
Joined: Sun Dec 20, 2009 5:10 pm

Re: get image from mysql database using php

Post by hero789 »

hi nga

After trying to use a database to store the actual image, I have instead placed the directory path into the database as loading an image from the database took too long.
Post Reply