Jump to content

How can I display images????


frank_solo

Recommended Posts

I read everywhere where it says not to stoe images in MYSQL but I have a code where I'm trying to display the images. They are all jpegs that are stored in the table. Here is the code

<?php
include "dbaptsConfig.php";
include "searchaptsstyle.css";
// test id, you need to replace this with whatever id you want the result from
$id = "1";

// what you want to ask the db
$query = "SELECT * FROM `apartments` WHERE `id` = ".$id;

// actually asking the db
$res = mysql_query($query, $ms);

// recieving the answer from the db	(you can only use this line if there is always only one result, otherwise will give error)	
$result = mysql_fetch_assoc($res);

// if you uncomment the next line it prints out the whole result as an array (prints out the image as weird characters)
// print_r($result);		

// print out specific information (not the whole array)			
  	echo "<br/>";
echo "<div id='title'>".$result['title']."<br/></div>";
echo "<br/>";
  	echo "<div id='description'>".$result['description']."<br /></div>"; 
echo "<br/>";
echo "<div id='table'><tr>"; 	
echo "<td bgcolor='#FFFFFF' style='color: #000' align='center'> Provider's Phone Number: ".$result['phone']."<br /></td>";
echo "<td bgcolor='#FFFFFF' style='color: #000' align='center'> Provider: ".$result['service']."<br /></td>";
echo "<td bgcolor='#FFFFFF' style='color: #000' align='center'> Borough: ".$result['county']."<br /></td>";
echo "<td bgcolor='#FFFFFF' style='color: #000' align='center'> Town: ".$result['town']."<br /></td>";
echo "<td bgcolor='#FFFFFF' style='color: #000' align='center'> Bedrooms: ".$result['rooms']."</td>";
echo "<td>   </td>";
echo "<td bgcolor='#FFFFFF' style='color: #000' align='center'> Bathrooms: ".$result['bath']."<br /></td>";
echo "<td bgcolor='#FFFFFF' style='color: #000' align='center'> Square Footage: ".$result['square']."<br /></td>";
echo "<td bgcolor='#FFFFFF' style='color: #000' align='center'> Rent: ".$result['rent']."<br /></td>";
echo "<td bgcolor='#FFFFFF' style='color: #000' align='center'> Listed On: ".$result['time']."<br /></td>";
echo "</tr></div>";
header("Content-type: image/jpeg");
echo "<td bgcolor='#FFFFFF' style='color: #000' align='center'> Listed On: ".$result['image1']."<br /></td>";
?>

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/231801-how-can-i-display-images/
Share on other sites

Thanks Kadeous but I've tried this and nothing.

<?php
include "dbaptsConfig.php";
include "searchaptsstyle.css";
// test id, you need to replace this with whatever id you want the result from
$id = "1";
$image1 = "image1";

// what you want to ask the db
$query = "SELECT * FROM `apartments` WHERE `id` = ".$id;

// actually asking the db
$res = mysql_query($query, $ms);

// recieving the answer from the db	(you can only use this line if there is always only one result, otherwise will give error)	
$result = mysql_fetch_assoc($res);

// if you uncomment the next line it prints out the whole result as an array (prints out the image as weird characters)
// print_r($result);		

// print out specific information (not the whole array)			
  	echo "<br/>";
echo "<div id='title'>".$result['title']."<br/></div>";
echo "<br/>";
  	echo "<div id='description'>".$result['description']."<br /></div>"; 
echo "<br/>";
echo "<div id='table'><tr>"; 	
echo "<td bgcolor='#FFFFFF' style='color: #000' align='center'> Provider's Phone Number: ".$result['phone']."<br /></td>";
echo "<td bgcolor='#FFFFFF' style='color: #000' align='center'> Provider: ".$result['service']."<br /></td>";
echo "<td bgcolor='#FFFFFF' style='color: #000' align='center'> Borough: ".$result['county']."<br /></td>";
echo "<td bgcolor='#FFFFFF' style='color: #000' align='center'> Town: ".$result['town']."<br /></td>";
echo "<td bgcolor='#FFFFFF' style='color: #000' align='center'> Bedrooms: ".$result['rooms']."</td>";
echo "<td>   </td>";
echo "<td bgcolor='#FFFFFF' style='color: #000' align='center'> Bathrooms: ".$result['bath']."<br /></td>";
echo "<td bgcolor='#FFFFFF' style='color: #000' align='center'> Square Footage: ".$result['square']."<br /></td>";
echo "<td bgcolor='#FFFFFF' style='color: #000' align='center'> Rent: ".$result['rent']."<br /></td>";
echo "<td bgcolor='#FFFFFF' style='color: #000' align='center'> Listed On: ".$result['time']."<br /></td>";
echo "</tr></div>";
?>
<html>
<body>
<img src="<?php echo $image1 ;?>" width="200">
</body>
</html>

You set $image1 to equal the string "image1" and then you tried echoing that into your <img> tag, so essentially what you printed to the browser was:

 

<img src="image1" width="200">

 

What kadeous was saying is to store the location of the image in your database, not the image itself.  Say you wanted to use the image located at "http://www.yoursite.com/images/test.gif".  In your database, you could store this as just a filename and location, and then echo that to your <img> tag.

 

Your database would look something like:

 

 ------ -------------------- 
| id   | location           |
------ --------------------
| 0    | 'images/test.gif'  |
| 1    | 'images/test2.gif' |
------ --------------------

 

And then you would select it from the DB in the same way as you do the rest of your data, and print it like this:

 

<img src="<?php echo $result['location']; ?>" width="200">

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.