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
getting image from database
Moderator: General Moderators
Picture data in a blob field?
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.
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.
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!!!!
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)
{
?>
<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>
<?
}
else
{
?>
<p>
<font size="-1">That press release could not be located in our database.</font>
<?
}
// close database connection
?>
<!-- page footer - snip -->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):
This will create an image tag wich points to the file mentioned in database.
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){
echo "<IMG SRC="images/$row->picture">";
}argh, That didn't work either
Any other ideas?
A-HA!
whooo-hooo! I got it. Here it is in case anyone else ever needs it.
Code: Select all
<img src="../images/<? echo $row->photo; ?>">