Forum Moderators: coopster

Message Too Old, No Replies

How can i get a picture from my database

         

skoff

12:58 pm on Feb 27, 2009 (gmt 0)

10+ Year Member



Hi!

I don't know how to do this simple thing! I just want that if the value of my "field2" is = to "bob" in my database i want to show the image of "bob.jpg". How can i do this? Thanks a lot! :)

blang

1:28 pm on Feb 27, 2009 (gmt 0)

10+ Year Member



Is the image stored in the database? Or you simply want to show an image from the local file structure based on a name in the field `field2`?

skoff

2:07 pm on Feb 27, 2009 (gmt 0)

10+ Year Member



I want to show an image from the local file structure based on a name in the field `field2` please

blang

2:43 pm on Feb 27, 2009 (gmt 0)

10+ Year Member



Ok, should be no problem. You want to output an IMG tag using the data from the field as a source,e.g. "bob". Assuming that the image name exactly matches the field data + ".jpg" (or whatever image format you choose), you can do something very simple like this in the SELECT statement:

SELECT
field1, field2, CONCAT( field2, ".jpg" ) AS imageurl
FROM sometable
ORDER BY somefield

Notice I'm using MySQL's CONCAT function to take the string in the `field2` field and append the .jpg extension onto it. Then I use an ALIAS in the SELECT so that the "bob.jpg" string (or whatever) that the CONCAT function creates has a name you can access in your resultset.

You can do a similar thing in PHP with just the concatenation operator ".", after the SELECT statement has pulled down the `field2` field data, e.g.


// assumes a valid query statement and resultset here
while( $row= mysql_fetch_assoc($result) ) {
$imageurl= $row['field2'] . ".jpg";
echo "<img src=\"$imageurl\" />";
}

This all assumes, of course, that you're using MySQL, but it can be easily adapted to any database.

skoff

3:24 pm on Feb 27, 2009 (gmt 0)

10+ Year Member



thks a lot!