Jump to content

Retrieving file location from database


RobertFullmen

Recommended Posts

Hey all,

I've written a php search feature for a mysql database. The search returns a file name like sample.gif, how would I go about displaying the actual image instead? Since the images all have the same location could I have the search return the string into a variable then make the whole thing a link? Thanks.

Link to comment
https://forums.phpfreaks.com/topic/232922-retrieving-file-location-from-database/
Share on other sites

Hi

 

More of a php question really.

 

However something like this would be required.

 

$sql = 'SELECT ImageName, ImageAltTag FROM Images';

$result = mysql_query($sql);

while($row = mysql_fetch_assoc($result))
{
echo '<img src="'.$row['ImageName'].'" alt="'.$row['ImageAltTag'].'" /><br />';
}

 

That will put out all the images on the database onto the screen.

 

If the images are in a standard sub directory somewhere (say 'images') them just change it to

 

$sql = 'SELECT ImageName, ImageAltTag FROM Images';

$result = mysql_query($sql);

while($row = mysql_fetch_assoc($result))
{
echo '<img src="images/'.$row['ImageName'].'" alt="'.$row['ImageAltTag'].'" /><br />';
}

 

All the best

 

Keith

That's what you should get; the result is a resource. What is showing up in the <img tag in the html source when you load the page and View ---> Source?

 

I'm getting nothing

 <img src="images/" alt="" /><br />

 

somewhere I'm loosing the data.

I got it! Thanks so much for the help everyone. I was over thinking it a bit, here's what worked:

$data = mysql_query("select * FROM Inventory WHERE img1 LIKE 'Searchterm'")
or die(mysql_error()); 


while($info = mysql_fetch_array( $data )) 
{ 
echo '<img src="images/'.$info['img1'].'"><br />';
} 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.