How to get my function to display images from my database??

Posted on 2011-04-23
Last Modified: 2012-06-27
Hi, I am trying to get images from my database table and display them on my page.  Its displaying something. . .

Can anyone see what I am missing in order to make this happen??

I call it like this on my page:

$images = new Image();

function image_path() {
	return $this->upload_dir.DS.$this->filename;
function display_images() {
	global $db_object;
	$images = $db_object->find_all();
	foreach($images as $image):
		echo "<a href=\"photo.php?id=" . $this->id . "\">";
		echo "<img src=\"" . $this->image_path() . "\" /></a>";
		echo $this->caption;
function find_all() { // for arrays
	return $this->find_by_sql("SELECT * FROM " . $this->table);
function find_by_sql($sql="") { // for arrays
	global $database;
	$result = $database->query($sql);
	$object_array = array();
	while ($row = $database->fetch_array($result)) {
		$object_array[] = $this->instantiate($row);
	return $object_array; // Must return an array!
private function instantiate($record) {
	$class_name = __CLASS__;
	$object = new $class_name; // Here is where it starts a new class for itself
	foreach($record as $attribute => $value) {
		if($object->has_attribute($attribute)) {
			$object->$attribute = $value;
	return $object;

Open in new window

Question by:FairyBusiness
    LVL 12

    Accepted Solution

    LVL 107

    Expert Comment

    by:Ray Paseur
    Some resources to help you learn the basics of PHP programming:

    The script fragments you posted above tell me that you need to step back from this project and get some kind of foundation in the language basics.  Posting a fragment of a class without the class definition is the kind of thing that reveals a fundamental lack of understanding about the basics of programming.  Confusing the terminology about displaying images vice displaying image links is another clue.  There is nothing wrong with being new to PHP programming -- we all were at one time or another.  But it is not reasonable to believe that you will learn PHP programming by posting questions in a forum, any more than you can learn to play the piano by asking pianists how they play.  Try to find a course in PHP, maybe at a local junior college, where you can have some structured lessons.  If you can't  get that, use the resources posted above so you can get some foundation in the language and the craft of programming.  The SitePoint book is especially good.

    And not to be facetious, but Google is your friend when you have very basic questions like this:

    Good luck with it, ~Ray

    Author Comment

    Yes, you have already pointed out in other questions that I am a beginner.  I already know this, and I have books, tutorials, and websites for help as well but sometimes I don't get it still so I come here.  Please post new or relevant stuff to my questions. . .

    Author Closing Comment

    LVL 12

    Expert Comment

    by:Mohamed Abowarda
    Glad to help :)

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    As this topic comes over and over again in different forms, I've finally decided to write a short (yea, right...) article / tutorial about pagination with PHP with MySQL database. There are dozens of these kind of tutorials, I know - I wanted to mak…
    Both Easy and Powerful How easy is PHP? (  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now