Jump to content

Archived

This topic is now archived and is closed to further replies.

newb

Doenst show my else echo cmd

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.

Share this post


Link to post
Share on other sites
Because the query failed, most likely. Use this query:

[code]$sql = "SELECT * FROM `thumbs` WHERE id='$id'";[/code]

Also change this
[code]if ( $id == "$imageshow") {[/code]

to this:
[code]if ( $id == $imageshow) {[/code]

Share this post


Link to post
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]

Share this post


Link to post
Share on other sites
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!)

Share this post


Link to post
Share on other sites
Ohhhh...yep that worked! Thanks a bunch fellas!

Share this post


Link to post
Share on other sites

×

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.