Page 1 of 1

get image from mysql database using php

Posted: Sun Dec 20, 2009 5:14 pm
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.

Re: get image from mysql database using php

Posted: Sun Dec 20, 2009 5:35 pm
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.

Re: get image from mysql database using php

Posted: Sun Dec 20, 2009 8:29 pm
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'];
 

Re: get image from mysql database using php

Posted: Sun Dec 20, 2009 11:47 pm
by nga
why dont you just input the img path into the database and echo the path into <img src="path"/>?

Re: get image from mysql database using php

Posted: Mon Dec 21, 2009 6:40 am
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.