By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,157 Members | 1,001 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,157 IT Pros & Developers. It's quick & easy.

Display image from MySQL (BLOB) from HTML Page using IMG Tag.

P: 1
I'm trying to display an image on my website using the following 2 pages which results in the big red x and not the image being displayed. The problem is that I'm never getting to my PHP page from my HTML page (img src=). What/how do I get the Test.php page to execute/open from my HTML page?

HTML Page:
----------
Expand|Select|Wrap|Line Numbers
  1. <body>
  2.  
  3. <img src="Test.php?passkey=1" alt="Image 1">
  4.  
  5. </body>
  6.  
  7. PHP Page:
  8. ----------
  9. <?php
  10. .
  11. .
  12. $result = mysql_query("SELECT Img_Auto_Id, Img_Image FROM tblImages WHERE CouId ='1'");
  13. .
  14. .
  15. $row = mysql_fetch_array($result);
  16. .
  17. .
  18. $txtImgId         = $row[0];
  19. $txtImgImage         = $row[1];  
  20.  
  21. header('Content-type: image/jpg'); 
  22. echo $txtImgImage;
  23. .
  24. .
  25. ?>
Mar 8 '12 #1
Share this Question
Share on Google+
1 Reply


PsychoCoder
Expert Mod 100+
P: 465
Take a look at this code, it should do what you're looking for:


Expand|Select|Wrap|Line Numbers
  1. //make sure there is an image name provided
  2. if(isset($_REQUEST[imname])
  3. {
  4.         //connect to the db
  5.         $db = mysql_connect("localhost", "username", "password") or die("Could not connect: " . mysql_error());
  6.  
  7.         //select our database
  8.         mysql_select_db("eventman") or die(mysql_error());
  9.  
  10.         //get the image name we're after
  11.         $image = stripslashes($_REQUEST[imname]);
  12.  
  13.         //build our query
  14.         //NEVER use select *
  15.         $sql_query = ""SELECT Img_Auto_Id, Img_Image FROM tblImages WHERE CouId ='1'";
  16.  
  17.         //get the results of the query
  18.         $query_results = mysql_query("$sql_query") or die("Invalid query: " . mysql_error());
  19.  
  20.         // set the header for the image
  21.         header("Content-type: image/jpeg");
  22.         echo '<img  src="'.mysql_result($query_results, 0).'">';
  23.         // close the db link
  24.         mysql_close($db);
  25. }
  26. else
  27. {
  28.         echo "Image name not provided!";
  29. }
Mar 10 '12 #2

Post your reply

Sign in to post your reply or Sign up for a free account.