954,587 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

displaying an image from a database to my website

hi there im new to php and im currently building my website .,it's more like an online store and i would like my products to be listed in a gridview., i have a database and my only problem is how to display the images .., i've already displayed some info like the id and some description ., ive been searching the web for the right codes and tutorials that could help me about this but nothing seem to suit my site..,

my database contains: petid(int), petname(varchar),birthdate(date), image(blob)

here is my connection.php

<?php
    $dbhost = 'localhost';
    $dbuser = 'root';
    $dbpass = '';
    $db = 'store';

    $conn = mysql_connect($dbhost,$dbuser, $dbpass);
    mysql_select_db($db);
?>


then here is my code inserted in an html page to retrieve the database contents

<?php
                  include 'includes/connection.php' ;

                    $query = ("SELECT * FROM pets");
              
                    $result = mysql_query($query);
              
                  

                    while($dog = mysql_fetch_array($result)){
                        $petid = $dog ['petid'];
                        $petname = $dog ['petname'];
                        $Birthdate = $dog ['Birthdate'];
                        
                        echo "<h3>" . $dog['petid'] ."</h3>";
                        echo "<P>" . $dog['petname'] ."</P>";
                        echo "<P>" . $dog['Birthdate'] ."</P>";

                    }
                ?>


any help would be appreciated :$ or if anyone here who have any alternative on displaying the image... ive heard about image pointers stored in the database would be much easier but i dont know how it works..

xiiopao
Newbie Poster
18 posts since Apr 2011
Reputation Points: 10
Solved Threads: 0
 

See you can work this out in one way....store all images in a folder and in database add location of image (either relative or absolute)...
and then just display ie using the below code...

<?php
                  include 'includes/connection.php' ;
 
                    $query = ("SELECT * FROM pets");
 
                    $result = mysql_query($query);
 
 
 
                    while($dog = mysql_fetch_array($result)){
                        $petid = $dog ['petid'];
                        $petname = $dog ['petname'];
                        $Birthdate = $dog ['Birthdate'];
 
                        echo "<h3>" . $dog['petid'] ."</h3>";
                        echo "<P>" . $dog['petname'] ."</P>";
                        echo "<P>" . $dog['Birthdate'] ."</P>";
                        echo "<img src='".$dog['image']."' alt='dog' width='200' height='200' />";
                    }
                ?>


Hope this is what you were looking for...PS-If your problem is solved mark the thread as solved....
Click on this to mark the thread as solved..

tomato.pgn
Posting Whiz in Training
262 posts since Mar 2011
Reputation Points: 4
Solved Threads: 32
 

tomato.pgn ...
do you mean i will change the blob data type to varchar then put something like "C:\wamp\www\myproj\images\pets\pup1.jpg" to the image value?

sorry im really new to this..

xiiopao
Newbie Poster
18 posts since Apr 2011
Reputation Points: 10
Solved Threads: 0
 

yes that is what i am telling you...change it to varchar...
and you don't have to give complete location of image ...you can use relative location...
i.e.,"./images/pets/pup1.jpg"....
Hopefully it's clear...

tomato.pgn
Posting Whiz in Training
262 posts since Mar 2011
Reputation Points: 4
Solved Threads: 32
 

yes it is thanks =) .., but i've tried it and my image didn't appear ... what appeared is a box with an 'x' mark ...

xiiopao
Newbie Poster
18 posts since Apr 2011
Reputation Points: 10
Solved Threads: 0
 

just put the last name in the database, as in imagename.jpg - it then should work :)

rhiang1984
Newbie Poster
3 posts since Apr 2011
Reputation Points: 10
Solved Threads: 1
 

and if that doesnt work, i will put a php code into here to help you get the image the way i did, you using php mysql as in phpmyadmin - university server or uni server as it knownto do database ?

rhiang1984
Newbie Poster
3 posts since Apr 2011
Reputation Points: 10
Solved Threads: 1
 

i figured it out!!! thanks tomato.pgn =)

xiiopao
Newbie Poster
18 posts since Apr 2011
Reputation Points: 10
Solved Threads: 0
 

tnx for the reply rhiang1984 =) the problem is my path... something with the slashes ... anyways the code worked and finally got my image display on the page thanks gusys =)

xiiopao
Newbie Poster
18 posts since Apr 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: