mcfmullen Posted January 30, 2010 Share Posted January 30, 2010 I've seen a lot of information regarding storing a complete URL into MySQL an then calling it through php to show an image. This makes sense to me. What I want to know, and I'm sure it will be easy to answer, is if I can only store a PART of a url in MySQL and call it through php... EX: Image url is http://www.website.com/directory/image.png Instead of storing that into a field in MySQL, I want to store ONLY image.png I then want to use php to call that bit into a given URL. The reason being that if ever I relocate the image directory or change domain, I can simply edit the php file once rather than rename all of the values in MySQL. So the question is: how can I call the variable into an incomplete URL in order to make the image display? Link to comment https://forums.phpfreaks.com/topic/190315-mysql-display-image/ Share on other sites More sharing options...
taquitosensei Posted January 30, 2010 Share Posted January 30, 2010 $image="image.png" // this is the image stored in your database $path="/var/www/html/images/"; this is the path to your images folder <img src='<?php echo $path.$image; ?>'> Link to comment https://forums.phpfreaks.com/topic/190315-mysql-display-image/#findComment-1004060 Share on other sites More sharing options...
teamatomic Posted January 30, 2010 Share Posted January 30, 2010 A url would display an image, a path is broken. $img='img.png'; $url='http://domain.com/'; echo "<img src=\"$url$img\"> HTH Teamatomic Link to comment https://forums.phpfreaks.com/topic/190315-mysql-display-image/#findComment-1004070 Share on other sites More sharing options...
mcfmullen Posted February 12, 2010 Author Share Posted February 12, 2010 The image.png part is actually stored in mysql: I have this: $url='http://www.mydomain.com/'; echo "Photo: <img src=\"$url".$row['photoAnimal'].\"<br/>"; But I'm getting errors. Can anyone correct my syntax? Thank you. Link to comment https://forums.phpfreaks.com/topic/190315-mysql-display-image/#findComment-1011144 Share on other sites More sharing options...
mapleleaf Posted February 12, 2010 Share Posted February 12, 2010 Try this: echo 'Photo: <img src="'.$url.$row['photoAnimal'].'"><br/>'; Link to comment https://forums.phpfreaks.com/topic/190315-mysql-display-image/#findComment-1011147 Share on other sites More sharing options...
mcfmullen Posted February 12, 2010 Author Share Posted February 12, 2010 I still get a syntax error. The code is part of a larger string being called by a MySQL table through an array with a loop: $result = mysql_query("SELECT * FROM Animals"); $url='http://www.mydomain.com/'; while($row = mysql_fetch_array($result)){ echo "ID: ".$row['idAnimal'].", Photo: <img src="'.$url.$row['photoAnimal'].'">, Name: ".$row['nameAnimal']."<br/>"; } No matter where I put the ', I keep getting a syntax error. Any advice? Link to comment https://forums.phpfreaks.com/topic/190315-mysql-display-image/#findComment-1011183 Share on other sites More sharing options...
sader Posted February 12, 2010 Share Posted February 12, 2010 echo "ID: ".$row['idAnimal'].", Photo: <img src='".$url.$row['photoAnimal']."'>, Name: ".$row['nameAnimal']."<br/>"; Link to comment https://forums.phpfreaks.com/topic/190315-mysql-display-image/#findComment-1011188 Share on other sites More sharing options...
mcfmullen Posted February 13, 2010 Author Share Posted February 13, 2010 Success, thank you! Link to comment https://forums.phpfreaks.com/topic/190315-mysql-display-image/#findComment-1011593 Share on other sites More sharing options...
mcfmullen Posted February 13, 2010 Author Share Posted February 13, 2010 I've been trying to get my information to show in a table: $result = mysql_query("SELECT * FROM Animals"); $url='http://www.mcfilmmakers.com/wp-content/farmville/animals/'; $results_array = array(); while ($row = mysql_fetch_array($result)) { $results_array[$row['idAnimal']] = $row; } ?> <table> <?php foreach ($results_array as $id => $record) { ?> <tr> <td> <?php echo $id;?> </td> <td> [color=red]<img src='<?php echo $url$row['photoAnimal']; ?>'>[/color] </td> <td> <?php echo $record['nameAnimal'];?> </td> </tr> <?php } ?> </table> The result won't display what is inside the $row['photoAnimal'] variable thus giving me broken image links. $id and $record['nameAnimal'] display just fine. Any pointers? I've tried using the other suggestions, even the one that had worked in my previous attempt, but even that won't work here. Link to comment https://forums.phpfreaks.com/topic/190315-mysql-display-image/#findComment-1011656 Share on other sites More sharing options...
mapleleaf Posted February 13, 2010 Share Posted February 13, 2010 <?php echo $url$row['photoAnimal']; ?> should be: <?php echo $url.$row['photoAnimal']; ?> Link to comment https://forums.phpfreaks.com/topic/190315-mysql-display-image/#findComment-1011830 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.