Jump to content

Echo'ing an Image from a DB Table?


stublackett

Recommended Posts

Hi,

 

I've got images inserted into my DB and its outputting the text no problem and even when I do echo $myrow['image']; its outputting the root string for the image in text i.e images/image.jpg

 

But it just wont show the Image when I put it into the <img src="<?php echo $myrow['image]" />

 

Any ideas why?

 

Heres my code

<?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> News Title:</b> ";

               echo $myrow['title'];
               
               echo "<br><br><b>News Posted 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>";
               // 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"){ 
                        
			        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>  
</div>
<div id="footer"></div>
<div id="left-menu">
<div id="side-nav">
<h1>Design & Technology</h1>
<a href="dt_announcements.php">Announcements</a>
<a href="#">Learning Materials</a>
<h1>English</h1>
<a href="english_announcements.php">Announcements</a>
<a href="#">Learning Materials</a>
<h1>Humanities</h1>
<a href="humanities_announcements.php">Announcements</a>
<a href="#">Learning Materials</a>
<h1>ICT</h1>
<a href="ict_announcements.php">Announcements</a>
<a href="">Learning Materials</a>
<h1>Mathematics</h1>
<a href="maths_announcements.php">Announcements</a>
<a href="#">Learning Materials</a>
<h1>Modern Foreign Languages</h1>
<a href="mfl_announcements.php">Announcements</a>
<a href="#">Learning Materials</a>
<h1>Physical Education</h1>
<a href="pe_announcements.php">Announcements</a>
<a href="#">Learning Materials</a>
<h1>Science</h1>
<a href="science_announcements.php">Announcements</a>
<a href="#">Learning Materials</a>
</div>
</div>
<div id="copyright"> Stuart Blackett</div>
<img src="<?php echo $myrow['image'] ; ?>" alt="News" class="news1"/>
</div>

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

First thing I notice is that

<img src="<?php echo $myrow['image'] ; ?>" alt="News" class="news1"/>

isn't inside your WHILE loop...

 

I've tried defining it in the WHILE loop as $img = $myrow['image'];

 

Then I've done <img src="<?php echo $img ; ?>" />

 

But no joy, How on earth do I get that Image echo'ing into the area  ???

Just an update

 

I spotted my error was with the routing of the file in the database, I had it as /images/image.jpg

One too many "/"'s

 

My next issue is somewhat similar, The image is in the Database with its URL string as news/pic3.jpg

 

The PHP is also echoing the variable $img; no problems, Its saying it is : news/pic3.jpg

 

So when I try <img src="<?php echo $img ; ?>"/> It just doesnt load "news/pic3.jpg"

 

If you change the img src to : <img src="news/pic3.jpg"> It loads the picture no problem, I just dont get it

 

*Scratches head*

???

 

<?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>";
                             
                $img = $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>";               
                				 				  
             }
             }
?>  
<img src="<?php echo $img ; ?>" alt="News Image" class="news1"/>

 

Once again, the IMG is outside the loop. So, the loop will run, each time setting $img to what is in the DB. Then, after the loop, it will show 1 image and it should be the value of the last row from the loop. Try the code real quick and see if the image URLs are printing correctly:

 

<?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);
  while($myrow = mysql_fetch_assoc($result)){//begin of loop
    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 "<b><td>Image URL:</b><br><br></td>";
    echo $myrow['image'];                                                             			       
    echo "<br>";               
    echo "<b><td>Image:</b><br><br></td>";
    echo "<img src=\"{$myrow['image']}\" />;                                                             			       
    echo "<br>";               
    echo "<hr>";
//    $img = $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>";               
    }
  }
?>

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.