How will I show Image from the batabase?

images php coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
User avatar
php-forum Fan User
php-forum Fan User
Posts: 979
Joined: Thu Feb 17, 2011 6:52 am
Location: Racine, WI

Tue Feb 22, 2011 9:33 am

How are you storing the image in the database?

Are you just storing the URL to an image stored elsewhere?

New php-forum User
New php-forum User
Posts: 6
Joined: Mon Feb 28, 2011 11:45 pm

Tue Mar 01, 2011 12:31 am

I hope you do not have a problem of inserting images in a table. You can get thumbnails or gallery of pictures from your database. However, note that I use a separate folder to keep the images and their pointers in the database. For example, the following code shows you that I have a table named audio_pics with all the columns as image_id and image_date. While fetching the image id and date from the table, it also anchors the image from a subfolder cd4car_images/thumbs/.

//get the thumbs
$getpic = mysql_query("SELECT * FROM audio_pics")
or die(mysql_error());
$records = mysql_num_rows($getpic);

while($rows = mysql_fetch_array($getpic)) {
echo "<tr>\n";
echo "<td><a href=\"../cd4car_images/thumbs/$image_id.jpg\" >";//this href is anchored to the image below
echo "<img src=\"../cd4car_images/thumbs/$image_id.jpg\" >";
echo "</a></td>\n";
echo "<td>" . $image_date . "</td>\n";
echo "</tr>\n";

if ($records == 0) echo "No results found. Please try again";

Let me hear from you, if I have to explain more.

User avatar
php-forum GURU
php-forum GURU
Posts: 2192
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT

Wed Mar 02, 2011 10:28 am

First and foremost.
mysql_fetch_array will fetch your data, with a numberic array starting at 0.
so your array would look something like this..

$row['0'] = "Something";
$row['1'] = "Something else";
$row['2'] = "mypicture.jpg";

mysql_fetch_assoc will fetch your data with the table fields as the array key..

$row['id'] = "Something";
$row['else'] = "Something else";
$row['image'] = "mypicture.jpg";


Then you would call the variable like so..

<image src="/the/directory/$row[image]" border="0" />

User avatar
php-forum Fan User
php-forum Fan User
Posts: 979
Joined: Thu Feb 17, 2011 6:52 am
Location: Racine, WI

Thu Mar 03, 2011 9:08 am

actually mysql_fetch_array allows you to reference row data by EITHER the index or the field name.

mysql_fetch_assoc only allows for field name and mysql_fetch_row only allows for index. This makes these two slightly faster than mysql_fetch_array.

In order of speed it is:

fetch_row is faster than fetch_assoc which are both faster than fetch_array.

Post Reply
  • Information
  • Who is online

    Users browsing this forum: No registered users and 1 guest