-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

5
  • 1
    No, echo $cname; before the switch. Or maybe var_dump($cname); Commented Feb 21, 2017 at 16:51
  • i get this string(4) "CASH" but it does not echo anything from the DB Commented Feb 21, 2017 at 17:07
  • and with echo i get CASH which means yes indeed it should be working but for some reason its not Commented Feb 21, 2017 at 17:15
  • $cname is only ever going to be whatever the last row of the database has it as. What is the value of $cname after the foreach loop, but before the switch statement? Commented Feb 21, 2017 at 17:41
  • before the foreach loop $cname does not exist Commented Feb 21, 2017 at 17:48

1 Answer 1

-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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

this does not change anything

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.