getting image from database

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
jmandango
Forum Newbie
Posts: 19
Joined: Wed Jun 05, 2002 10:39 am
Location: Michigan

getting image from database

Post by jmandango »

Hello, I am trying to have a "news" section on a site that will allow me to have a different "story" each month. Each story has a corresponding image that goes with it.

The table in the database has several fields: Title, Subtitle, Image, Body, etc... I have no problem pulling the text data (title, etc.) from the database into the story page but I have no idea how to pull the corresponding image from the database or from a folder via a reference tag in the db.

Any ideas?

thanks
User avatar
Beowulf
Forum Newbie
Posts: 3
Joined: Mon Jul 15, 2002 4:32 pm

Post by Beowulf »

What's the problem? You probably have some key field in your table. Make it "zerofill", so that you get "0000001" instead of "1". Then just write something like "<img src="/path/to/my/file/$my_key.jpg>", where $my_key is your key field. And all your images are stored in "/path/to/my/file/"
User avatar
haagen
Forum Commoner
Posts: 79
Joined: Thu Jul 11, 2002 3:57 pm
Location: Sweden, Lund

Picture data in a blob field?

Post by haagen »

Hi

I assume you mean that you have the picture in a blob field or something like that. I make another assumption as well, you have a primary key called idx (or what ever).

When you create the page with information form the database, you create an image tag that looks something like this:

<IMG SRC="mygetpictureformdbscript.php?picture=$idx">

In your "mygetpicturefromdbscript.php" you load the picture into a variable wich you print to the browser. Do not forget to print a correct mime header to the browser so it understands that it is a picture you print.

I hope you understand how I mean.
jmandango
Forum Newbie
Posts: 19
Joined: Wed Jun 05, 2002 10:39 am
Location: Michigan

Still can't figure it out.

Post by jmandango »

This is what I have thus far. I have no problem pulling in a title, subtitle etc. from the database -- I have no clue how to dynamically include a photo using a database.

Where do I put the images?
What do I put in the database (mySQL)?
What do I put in the php code (see below)?

Please help! THanks a bunch!!!!

Code: Select all

<?
// story.php - display contents of selected press release
?>

<!-- page header - snip -->

<?
//includes

// open database connection

// select database

// generate and execute query
$query = "SELECT photo, title, subtitle FROM table WHERE id = '$id'";
$result = mysql_query($query) or die ("Error in query: $query.".mysql_error());

// get resultset as object
$row = mysql_fetch_object($result);

// print details
if ($row)
&#123;
?>
	<p>
<!--THIS IS WHERE I WANT TO PUT THE IMAGE
		// it should pull a different image in
		// depending on row-->
		
	<p><? echo $row->title; ?><br>
	
	<? echo $row->subtitle; ?></p>
		
<?
&#125;
else
&#123;
?>
	<p>
	<font size="-1">That press release could not be located in our database.</font>
	<?
	&#125;
	
// close database connection

?>

<!-- page footer - snip -->
User avatar
haagen
Forum Commoner
Posts: 79
Joined: Thu Jul 11, 2002 3:57 pm
Location: Sweden, Lund

Post by haagen »

Ok. You're images are'nt in the database. Store them as files in a directory, call it for example "images".

In your database add a field called picture or something like that. This is where you store the name of the file. Then you can retrive the picture with this code (untested):

Code: Select all

if($row->picture)&#123;
  echo "<IMG SRC="images/$row->picture">";
&#125;
This will create an image tag wich points to the file mentioned in database.
jmandango
Forum Newbie
Posts: 19
Joined: Wed Jun 05, 2002 10:39 am
Location: Michigan

argh, That didn't work either

Post by jmandango »

Any other ideas?
jmandango
Forum Newbie
Posts: 19
Joined: Wed Jun 05, 2002 10:39 am
Location: Michigan

A-HA!

Post by jmandango »

whooo-hooo! I got it. Here it is in case anyone else ever needs it.

Code: Select all

<img src="../images/<? echo $row->photo; ?>">
Post Reply