Page 1 of 1

getting image from database

Posted: Tue Jul 16, 2002 9:58 am
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

Posted: Tue Jul 16, 2002 12:09 pm
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/"

Picture data in a blob field?

Posted: Tue Jul 16, 2002 12:41 pm
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.

Still can't figure it out.

Posted: Tue Jul 16, 2002 1:05 pm
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 -->

Posted: Tue Jul 16, 2002 1:16 pm
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.

argh, That didn't work either

Posted: Tue Jul 16, 2002 2:07 pm
by jmandango
Any other ideas?

A-HA!

Posted: Tue Jul 16, 2002 2:25 pm
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; ?>">