-1

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 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);

}
    }

i want to execute the case code if my $cname variable matches it, but instead it returns results from the table in the default code block. i looked at different posts here PHP switch not working as expected , here PHP switch "||" not working properly and here PHP switch case default

but they do not address my issue

Community
  • 1
  • 1
Morena
  • 7
  • 2
  • 8

1 Answers1

-1

It is because $cname is something different then CASH. Maybe it's case insensitive? run strtolower on $cname:

switch(strtolower($cname)) {
  case 'cash':
    echo 'got $$';
    break;
}
Ruben Vincenten
  • 807
  • 4
  • 7