Jump to content

Doenst show my else echo cmd


newb

Recommended Posts

[code]<?php
require "../config.php";

  $id = $_GET["id"];
      $sql = "SELECT * FROM `thumbs` WHERE id=$id";
      $result = mysql_query($sql); 
             
// Build Table
while( $row = mysql_fetch_assoc( $result ) ) {

$imageshow=$row['id'];

if ( $id == "$imageshow") {
echo '<br /><br /><a href="#" onclick="window.close()"><img class="displaypic" src="images/full/' . $row['category'] . '/' . $row['id'] . '.jpg"></img></a><br />';
  } else {
    echo "Image ID: $id Doesnt Exist";
    }
}
?>[/code]

thats my code, when i type in, say www.mysite.com/thumbs.php?id=4543535435 it wont say 'image id: 4543535435 doesnt exist' and i know it doesnt exist in the database.
Link to comment
https://forums.phpfreaks.com/topic/14806-doenst-show-my-else-echo-cmd/
Share on other sites

try this code newb.

[code]
<?php
require "../config.php";

    $id = $_GET["id"];
    $sql = "SELECT * FROM `thumbs` WHERE id='$id'";
    $result = mysql_query($sql);

    if (mysql_num_rows($result) == 0)
    {
        echo "Image ID: $id Doesnt Exist";
    }
    else
    {
        while( $row = mysql_fetch_assoc( $result ) ) {
            echo '<br /><br /><a href="#" onclick="window.close()"><img class="displaypic" src="images/full/' . $row['category'] . '/' . $row['id'] . '.jpg"></img></a><br />';
        }
    }
?>
[/code]
Do you get any output?

You really should be checking to see if you got any records returned before you go into the while loop. If there are no records returned your script will not put output anything. Try something like this:
[code]<?php
      $id = $_GET["id"];
      $sql = "SELECT * FROM `thumbs` WHERE id=$id";
      $result = mysql_query($sql) or die("Problem with the query: $sql<br>" . mysql_error()); 
      if (mysql_num_rows($result) == 0) // if no rows returned then id doesn't exist
          echo "Image ID: $id Doesnt Exist";
      else {
// Build Table
          while($row = mysql_fetch_assoc($result))
              echo '<br /><br /><a href="#" onclick="window.close()"><img class="displaypic" src="images/full/' . $row['category'] . '/' . $row['id'] . '.jpg"></img></a><br />';
      }
?>[/code]

Ken (I see hvle beat me to the same code -- GMTA!)

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.