Jump to content

[SOLVED] if statement not working correctly


LiamH

Recommended Posts

Hi all.

 

I'm adding bits and pieces to my website, and one improvement is going to be a image viewer.

 

I have an article table in my database which has a section where you can put the names of images that are to be used in the article. These images will then be printed underneath the article, and eventually have a link which will open up the image viewer, and display the images associated with that article.

 

So for example, the article table has column called screen1, just for testing purposes. In this there is a row that has the position of the image in the server. For example /images/image1.jpg. If no screenshots are required, the field is left blank.

 

Now in the actual code for the article page I have the following;

 

if (mysql_num_rows($res1) > 0) {
    
    while($row = mysql_fetch_assoc($res1)) {
	    echo stripslashes("<img src=".$row['screen1'].">");
	    
	    }
    
    
    }

 

Now there is evidently something wrong with what I have done somewhere, as the code automatically prints the <img> even if there is no image associated with the article in the database. Where have I gone wrong please?

why are you using strip slashes? also, you need an if on the field:

if (mysql_num_rows($res1) > 0) {
      
       while($row = mysql_fetch_assoc($res1)) {
          if($row['screen1']){
            echo "<img src=".$row['screen1'].">";
          }
       }     
    }

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.