Jump to content

Showing an Image pulled from a DB Table?


stublackett

Recommended Posts

Hi,

 

I've got a database setup with images in it, For news items

 

I'm using <img src="<?php echo $img ; ?>"/>

Now if I echo out $img in my code it shows the URL string to the image, So why on earth is it just not showing the image? I dont quite understand it, Any ideas?

 

My code for it is as follows

index.php

<?php #news-index.php By Stuart Blackett
include("dbconnect.php");
        $result = mysql_query("SELECT *, DATE_FORMAT( time, '%D %M %Y @ %H:%i' )AS uk_date FROM $db_table ORDER BY time DESC LIMIT 2",$connect);
                //Limit news items to 3
                                             
        while($myrow = mysql_fetch_assoc($result))           
                          
             {//begin of loop

               //now print the results:
              echo "<hr>";              
              echo "<br>";
              echo "<b>Title:</b> ";
              echo $myrow['title'];          
              echo "<br><br><b>News Added On :</b> ";
              echo $myrow['uk_date'];                           			         
              echo "<br> <br>";                        	         
		        echo "<b><td>News Description :</b><br><br></td>";
              echo $myrow['description'];                                                             			       
              echo "<br>";               
              echo "<hr>";
                              
              $image = $myrow['image'];
              
                                                                                       
               // Now print the options to (Read,Edit & Delete the news)
              echo "<br><a href=\"read_more.php?newsid=$myrow[newsid]\">Read More </a>";
                                          
if ($userlevel == "2" ||  $userlevel == "3"){ //Restrict Add, Edit & Delete from Students 
                        
			        echo "|| <a href=\"add_news.php\">Add News </a>";
			   
                echo "|| <a href=\"edit_news.php?newsid=$myrow[newsid]\">Edit </a>";

                echo "|| <a href=\"delete_news.php?newsid=$myrow[newsid]\">Delete </a><br><br>";               
                				 				  
             }
             }
?>  
</div>
<img src="<?php echo $img ; ?>"/>  
</div>

 

My suspicions are that the echo is below the IF statement so it might restrict it, I'm not sure

 

But I know for definate that the URL string is being pulled correctly and the image is definatley in that folder it is referring to

???

Link to comment
https://forums.phpfreaks.com/topic/97358-showing-an-image-pulled-from-a-db-table/
Share on other sites

1st: i highly recommend against storing images (or anything binary of significant size) in a database. store the image name; it solves a lot of potential problems. but assuming you're going to do it anyway:

 

you can't just echo the binary data as the img source. you will need to create a secondary script that pulls the image from the database and sends it to the browser appending the appropriate headers to indicate that the binary data is an image. for instance:

 

<IMG SRC='somescript.php?id=23'>

 

where somescript.php is a PHP script and id is the id of the image in the table. somescript.php will use the id to pull the image data, then send the proper headings for the image type followed by the binary data.

 

or better yet, just store the image name and put it in the <IMG> tag.

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.