Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    Jun 2010
    Location
    Earth
    Posts
    305
    Thanks
    27
    Thanked 2 Times in 2 Posts

    if else statement not working

    I am running this query and I need it to print out the "Nothing Found" when nothing is found but not sure where to put that else statement.

    I have tried to put it in both if's but does not work. Clearly I am not understanding how this works. CAn someone help me??

    PHP Code:
             $query = ("SELECT user.userID, user.gender, user.bd_year, user.city, user.state, photos.userID, photos.photo_1, CURDATE(), (YEAR(CURDATE())-YEAR(birth_date)) - (RIGHT(CURDATE(),5)<RIGHT(birth_date,5)) AS age
                        FROM user LEFT JOIN photos ON user.userID = photos.userID
                        WHERE user.bd_year <= $year1 AND user.bd_year >= $year2 AND user.gender = $genderPref "
    );

             if (
    $result mysql_query($query))
               {
                 echo 
    " Hmm ";

                 
    $c 0;
                 if (
    $num_rows != '0')
                   {
                     echo 
    " Do this stuff ";
                   }
               }
             else 
               {
                 echo 
    " Nothing found ";
               } 
    Right now it echos both the "1st if statement" & "2nd if statement" regardless of the search criteria.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    The statement indicates that your query itself has failed. The else branch is associated with the mysql_query, and probably shouldn't be indicated as a 'Nothing Found' answer.
    PHP Code:
    if ($result = @mysql_query($query))
    {
        echo 
    'hmmm';
        
    $c 0;
        if (
    mysql_num_rows($result) === 0)
        {
            echo 
    'There are no results found.';
        }
        else
        {
            echo 
    'Results has been found';
            while (
    $row mysql_fetch_assoc($result))
            {
                
    // Do something with $row.
            
    }
        }
    }
    else
    {
        die(
    'MySQL query failed: ' mysql_error());

    Of course you can capture any variables you want. Key things to remember are that the result of a mysql_query is a resource, not a collection of immediatly usable data. To make use of it, you need to use mysql_fetch_assoc, mysql_fetch_array, mysql_fetch_row, mysql_result, or evaluate other data with operations like mysql_num_rows.

    Edit:
    Sorry, your wanting it to echo nothing found, not that it is. My bad, the query itself looks like it successfully executed, so use the above to determine if nothing is found.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #3
    Senior Coder
    Join Date
    Jul 2005
    Location
    New York, NY
    Posts
    1,084
    Thanks
    4
    Thanked 19 Times in 19 Posts
    mysql_query returns FALSE on error ONLY, otherwise it returns something that evaluates to true.

    If your query executes successfully but pulls 0 rows, mysql_query will return something that evaluates to true.

    So your code should really be:

    Code:
    if ($result = mysql_query($query))
    {
      // success
      if (mysql_num_rows($result))
      {
         // query returned data
      }
      else
      {
         // query did not return data
      }
    }
    else
    {
      // handle error
    }

  • #4
    Regular Coder
    Join Date
    Jun 2010
    Location
    Earth
    Posts
    305
    Thanks
    27
    Thanked 2 Times in 2 Posts
    Thanks!

    I switched this line

    PHP Code:
    if ($num_rows != '0'
    to this and it works

    PHP Code:
    if (mysql_num_rows($result)) 

  • #5
    New to the CF scene
    Join Date
    Jun 2010
    Posts
    3
    Thanks
    0
    Thanked 1 Time in 1 Post
    you can use this


    $query = ("SELECT user.userID, user.gender, user.bd_year, user.city, user.state, photos.userID, photos.photo_1, CURDATE(), (YEAR(CURDATE())-YEAR(birth_date)) - (RIGHT(CURDATE(),5)<RIGHT(birth_date,5)) AS age
    FROM user LEFT JOIN photos ON user.userID = photos.userID
    WHERE user.bd_year <= $year1 AND user.bd_year >= $year2 AND user.gender = $genderPref ");

    if ($result = mysql_query($query))
    {
    echo " Hmm ";

    $c = 0;
    if (mysql_num_rows($result))
    {
    echo " Do this stuff ";
    }
    }
    else
    {
    echo " Nothing found ";
    }

  • Users who have thanked technolojik for this post:

    harkly (06-29-2010)

  • #6
    Regular Coder
    Join Date
    Jun 2010
    Location
    Earth
    Posts
    305
    Thanks
    27
    Thanked 2 Times in 2 Posts
    Thanks!!

    Got it working!!

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Quote Originally Posted by technolojik View Post
    you can use this

    PHP Code:
     $query = ("SELECT user.userID, user.gender, user.bd_year, user.city, user.state, photos.userID, photos.photo_1, CURDATE(), (YEAR(CURDATE())-YEAR(birth_date)) - (RIGHT(CURDATE(),5)<RIGHT(birth_date,5)) AS age 
                        FROM user LEFT JOIN photos ON user.userID = photos.userID 
                        WHERE user.bd_year <= $year1 AND user.bd_year >= $year2 AND user.gender = $genderPref "
    ); 

             if (
    $result mysql_query($query)) 
               { 
                 echo 
    " Hmm "

                 
    $c 0
               if (
    mysql_num_rows($result))
                   { 
                     echo 
    " Do this stuff "
                   } 
               } 
             else  
               { 
                 echo 
    " Nothing found "
               } 
    Ummm, no. This still suffers from a deficiency of not being able to detect a failure versus the number of items being less than or equal to 0. The only time nothing will be found is when there is an error in the SQL itself.

    You can however move the else to branch from the inner if instead of the outter if (as I've shown in post #2).
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #8
    Regular Coder
    Join Date
    Jun 2010
    Location
    Earth
    Posts
    305
    Thanks
    27
    Thanked 2 Times in 2 Posts
    I know, I stated above that I had been moving the else statement around because I couldn't get it to work. My main problem was the "if (mysql_num_rows($result)) " line.

    With that corrected I was able to get it to work.


  •  

    Posting Permissions

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