SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2003
    Location
    ohio
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question why does this condition never fail?

    here is the code snippet

    PHP Code:
            $query "SELECT linkid, linkdescrip, linkurl, linkowner FROM links WHERE linkurl = '$linkurl'";

            
    $result mysql_query($query);

            if(!
    $resulterror_message(sql_error());


            
    $query_data mysql_fetch_array($result);
            
    $linkid $query_data["linkid"];
            
    $linkurl $query_data["linkurl"];
            
    $linkdescrip $query_data["linkdescrip"];
            
    $linkowner $query_data["linkowner"];
            
    $query_data mysql_fetch_row($result);
            
    $total_num $query_data[0];

            if (!
    $result) {
                    echo 
    "record not found!";
                    exit;
                    }
                    else {
                    echo 
    "$result";
                    echo 
    "<h1>Showing link from  database</h1><p>";
                    echo 
    "<h2>Link Number: $linkid Url: $linkurl<br>Description: $linkdescrip<br>Link owner: $linkowner</br></h2$
                    echo "
    Total links$total_num"; 
    yet the condition never fails! shouldn't the result = 0 if the record can't be found?

  2. #2
    orange pips! orange pips!
    Join Date
    Oct 2001
    Location
    Its all about location
    Posts
    652
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There is information in $result because your query was executed successfully to the database. You should instead be checking if $result contains any selected rows. Do this by:

    PHP Code:
     if (mysql_num_rows($result) == 0

  3. #3
    does not play well with others frezno's Avatar
    Join Date
    Jan 2003
    Location
    Munich, Germany
    Posts
    1,391
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you are using the !$result twice. I don't think that can work properly.
    Also there's twice the $query_data, one with fetch_row and the other with fetch_array.
    COUNT(*) might be a help for $total_num.
    Whatever you want to do, to me this snippet looks kind of weird.
    We are the Borg. Resistance is futile. Prepare to be assimilated.
    I'm Pentium of Borg.Division is futile.Prepare to be approximated.

  4. #4
    Non-Member coo_t2's Avatar
    Join Date
    Feb 2003
    Location
    Dog Street
    Posts
    1,819
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by link-pimp
    here is the code snippet

    PHP Code:
            $query "SELECT linkid, linkdescrip, linkurl, linkowner FROM links WHERE linkurl = '$linkurl'";

            
    $result mysql_query($query);

            if(!
    $resulterror_message(sql_error()); 
    To get the behavior you expect you should check the number of rows returned as someone has suggested. But you could keep this line in if you wanted, as this would cause an error message if the query fails.
    Just keep in mind that a query doesn't have to return any rows in order to be successful.

    --ed

  5. #5
    SitePoint Enthusiast
    Join Date
    Sep 2003
    Location
    ohio
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for the instant answers guys! it works now, just like you said krayzie!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •