0

I have a simple sql statement and I would like to perform a different action depending on the number of rows returned.

$result_lists = mysql_num_rows(mysql_query("SELECT * FROM db_table"));
    //To see the number returned
    print_r($result_lists);

    switch($result_lists) {
         case($result_lists == 0):
         //To prove which option is actually happening 
         print_r('lists==0: '.$result_lists);  
         break;

         case($result_lists > 1):
         //To prove which option is actually happening 
         print_r('lists>1: '.$result_lists);
         break;

         case($result_lists == 1):
         //To prove which option is actually happening 
         print_r('lists==1: '.$result_lists);  
         break;
    }

If 1 or more row is found, then the correct case is used, however, if zero rows are returned, for some reason the (> 1) case is carried out.

Can anyone see what might be going wrong?

Any advice appreciated.

Thanks.

1
  • 1
    Please do not validate an answer that fast, let people answer. Btw this answer might work, but it's still a bad answer. Commented Sep 22, 2009 at 11:43

3 Answers 3

8

You're abusing the switch statement - you should replace it with ifs or change it like this:

switch ($result_lists)
{
     case 0:
         //To prove which option is actually happening 
         print_r('lists==0: '.$result_lists);  
         break;

     case 1:
         //To prove which option is actually happening 
         print_r('lists==1: '.$result_lists);  
         break;

     default:
         //To prove which option is actually happening 
         print_r('lists>1: '.$result_lists);
         break;
}

What you're doing at the moment is like this:

case($result_lists == 0):

// is like doing
if ($result_lists == ($result_lists == 0))

// which when $result_lists = 0 is the same as

if ($result_lists == true)
if (0 == 1)
// therefore false so it drops through to the next statement

case($result_lists > 1)
// the same as
if ($result_lists == ($result_lists > 1))
// when $result_lists = 0:
if ($result_lists == false)
if (0 == 0)
Sign up to request clarification or add additional context in comments.

1 Comment

+1 for more explanations than I gave. And I would add +1 more if I could for the robot-looking-line of code : if (0 == 0) :D
1

You shouldn't use switch like that.

switch($var)
{
    case 1:
        //Some stuff
        break;
    case 2:
        //Some stuff
        break;
    default:
        break;
}

It is the right way to do this. Use ifs and elses to do it, and yadayda ! Your bug will disappear.

Why ? Because case isn't made to evaluate statements. It only compare what's in the switch with what's in the case.

Comments

-4

Returns zero or null, you have check

case($result_lists == 0 or null):

or maybe

 case(empty($result_lists)):

1 Comment

switch statement doesn't work that way. Bad answer and -1 for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.