Jump to content

php switch only using the default code block


morena

Recommended Posts

I have a php switch statement and for some reason php switch is only executing the default code in the default block but not in the case block, i check if my variable is isset first, then use switch to select data from my DB, even if i do var_dump($cname); it shows that variable $cname corresponds with my first case, what is the problem here?

 here is the code

$sel = 1;
    $sql = "(select * from insurance_companies WHERE id='$eop' LIMIT $sel)";
    foreach ($pdo->query($sql) as $row){ 
    $cID = $row['id'];  
    $cname = $row['name'];          
    }
 if  (isset($cname)) {
                    switch ($cname) {
                        case 'CASH':
                            $query= "SELECT test FROM cash_procedures WHERE test LIKE '%$did%'";
                 $result= $con->query($query);
 break;

                        default:
                              $query= "SELECT test FROM procedures WHERE test LIKE '%$did%'";
                 $result= $con->query($query);

}
    }
Link to comment
Share on other sites

Then what you're describing is not possible.

 

1. You didn't mention where you put the var_dump, but assuming it came sometime after the (unnecessary) foreach loop then it shouldn't matter.

2. The crazy indentation suggests you pieced together your post from different pieces of code, rather than from one complete block.

3. You say it executed the default case - how do you know that? Have you put debugging statements (eg, an echo) in each case to see whether it really is executing that way?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.