how to display images stored in a mysql database using php

i am trying to display images stored in my database using php , it does not show any images it just fills the page with letters and symbols.
Can anyone help me

the code which needs fixing follows:

if (isset ($_POST[‘search_name’]) ){

$search_name = $_POST[‘search_name’];
if (!empty($search_name) ){
$query = “SELECT image FROM movies WHERE name LIKE '”.$search_name."%’ ";
$query_run = mysql_query($query);

if(mysql_num_rows($query_run) >= 1 ){
echo 'results found: ';
while($query_row = mysql_fetch_assoc($query_run) ) {

echo $query_row[‘image’].’
’;

}

}else{
echo ‘no results found’;
}

never store images in a database, its a huge waste of space and resources. If this is part of a social page or whatnot, then store the filename, then use the img tag like you normally would.

its just part of an assignment for college. do you know how i can fix my problem or can you give me a simple example of what you have suggested

I totally agree with Richei! Never store pictures inside of a database. It will slow everything down.

To further explain what Richei was talking about, here is the layout of how it would work.

Create a folder on your server called “images” or “pictures”, something that makes sense.
In your UPLOAD page, you just add code to upload a file to your folder that you created.
When you upload the picture file, you save the name of the file into your database as a string.

In your DISPLAY page which would display the picture, it would read your filenames from the
database and load the image using the “images/filename.jpg” onto the display page.

Loading files to a server is easy. Check out PHP.net for details, just search there for “upload file”
and also, there you will find code for saving strings to databases and retrieving them.

Please post here again when you get some code in place for us to help you with… Good luck

Its not a way i’d ever teach someone to do it, but give this a try - http://www.wellho.net/mouth/937_Display-an-image-from-a-MySQL-database-in-a-web-page-via-PHP.html

thanks for the help , i will try and get some code up and running and let you’s know if i do, thanks again

hi again , i set up a databse with 3 colums (id, name, and image), and i stored the image path in the image column. the code seems to work but i dont get the image i want, i just get sort of a picture icon instead, anyone any ideas? the code i’m using is below:

if (isset ($_POST[‘search_name’]) ){

$search_name = $_POST[‘search_name’];
if (!empty($search_name) ){
$query = “SELECT image FROM testimage WHERE name LIKE '”.$search_name."%’ ";
$query_run = mysql_query($query);

if(mysql_num_rows($query_run) >= 1 ){
echo 'results found: ';
while($query_row = mysql_fetch_assoc($query_run) ) {

$query_row[‘image’];

$image = $query_row[‘image’];

echo ‘’;

Are you storing the entire path or just the filename?

Hmmm, this code looks correct. Usually when you get a small icon instead of the actual picture, it is almost always due to not finding the picture. So, in our previous talks, I suggested creating a folder called “images” and putting the pictures in there. If you did that, you need to add “images/” to the beginning of the image name which is actually a name of an image. So instead of getting “pictureofdog.jpg”, you would get"images/pictureofdog.jpg"… Perhaps this is the problem.

Also, one quick way to check this, is to run the post, look at the results and RIGHT-CLICK on the page ans select VIEW-SOURCE. You will be able to look at the code that the PHP created for you to view the picture. In this way, you should be able to see what the code actually holds and I am sure it is something to do with that part. Good luck…

i’m storing the entire path(varchar 50)

Again, RIGHT-CLICK on your page in a blank area and select VIEW-SOURCE from the context menu that pops up.
Then, look where the picture is displayed and see where on your site it is trying to pull the picture from. Most likely, it is from the incorrect address of the picture…

ok checked the image path in the source code it is ok. i am using xammp and phpadmin on my laptop. i stored the images in a folder called images in my websites folder.
using the full path of the image in the database and its stored as a varchar 50 characters,
I’m really clueless on this now. displaying images on a php page through mysql db is by far the hardest thing i’ve done in php so far cant seem to get it working.

yesss got it to work ;D , just changed the full path to just the image name(name.jpg) in the database and it worked, something so simply.
thanks for all the help guys and advice to store path name in a database instead of the actually image

Glad you got it working… We will mark this one solved. I was sure it was the name of the file and the path issue. This is common… But, the easy ones are usually the hardest to find… Congrats!

Sponsor our Newsletter | Privacy Policy | Terms of Service