frank_solo Posted March 26, 2011 Share Posted March 26, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/231801-how-can-i-display-images/ Share on other sites More sharing options...
Skewled Posted March 26, 2011 Share Posted March 26, 2011 You store the location of the img in MySQL then use the HTML <img > tag and echo the variable. Quote Link to comment https://forums.phpfreaks.com/topic/231801-how-can-i-display-images/#findComment-1192643 Share on other sites More sharing options...
frank_solo Posted March 26, 2011 Author Share Posted March 26, 2011 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> Quote Link to comment https://forums.phpfreaks.com/topic/231801-how-can-i-display-images/#findComment-1192650 Share on other sites More sharing options...
nethnet Posted March 26, 2011 Share Posted March 26, 2011 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"> Quote Link to comment https://forums.phpfreaks.com/topic/231801-how-can-i-display-images/#findComment-1192652 Share on other sites More sharing options...
Skewled Posted March 26, 2011 Share Posted March 26, 2011 ^ That's correct sorry I didn't elaborate more. Just hard to do from a phone. But that will give you the results you were after. Have fun. Quote Link to comment https://forums.phpfreaks.com/topic/231801-how-can-i-display-images/#findComment-1192657 Share on other sites More sharing options...
frank_solo Posted March 26, 2011 Author Share Posted March 26, 2011 Its ok thank you to both for the help! Quote Link to comment https://forums.phpfreaks.com/topic/231801-how-can-i-display-images/#findComment-1192658 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.