RobertFullmen Posted April 6, 2011 Share Posted April 6, 2011 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 More sharing options...
kickstart Posted April 7, 2011 Share Posted April 7, 2011 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 Link to comment https://forums.phpfreaks.com/topic/232922-retrieving-file-location-from-database/#findComment-1198118 Share on other sites More sharing options...
RobertFullmen Posted April 8, 2011 Author Share Posted April 8, 2011 Thanks Keith, I was trying something very similar to what you posted. In both cases however the image doesn't show up. So I echoed $result and got "Resource id #2" which is what I got the first time... What could be the problem? Link to comment https://forums.phpfreaks.com/topic/232922-retrieving-file-location-from-database/#findComment-1198491 Share on other sites More sharing options...
Pikachu2000 Posted April 8, 2011 Share Posted April 8, 2011 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? Link to comment https://forums.phpfreaks.com/topic/232922-retrieving-file-location-from-database/#findComment-1198499 Share on other sites More sharing options...
RobertFullmen Posted April 8, 2011 Author Share Posted April 8, 2011 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. Link to comment https://forums.phpfreaks.com/topic/232922-retrieving-file-location-from-database/#findComment-1198509 Share on other sites More sharing options...
spiderwell Posted April 8, 2011 Share Posted April 8, 2011 can you post the code you have? Link to comment https://forums.phpfreaks.com/topic/232922-retrieving-file-location-from-database/#findComment-1198510 Share on other sites More sharing options...
RobertFullmen Posted April 8, 2011 Author Share Posted April 8, 2011 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 />'; } Link to comment https://forums.phpfreaks.com/topic/232922-retrieving-file-location-from-database/#findComment-1198514 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.