morena Posted February 22, 2017 Share Posted February 22, 2017 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); } } Quote Link to comment https://forums.phpfreaks.com/topic/303261-php-switch-only-using-the-default-code-block/ Share on other sites More sharing options...
requinix Posted February 22, 2017 Share Posted February 22, 2017 Did var_dump($cname) show exactly string(4) "CASH" Quote Link to comment https://forums.phpfreaks.com/topic/303261-php-switch-only-using-the-default-code-block/#findComment-1543204 Share on other sites More sharing options...
morena Posted February 22, 2017 Author Share Posted February 22, 2017 yes it did Quote Link to comment https://forums.phpfreaks.com/topic/303261-php-switch-only-using-the-default-code-block/#findComment-1543205 Share on other sites More sharing options...
requinix Posted February 22, 2017 Share Posted February 22, 2017 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? Quote Link to comment https://forums.phpfreaks.com/topic/303261-php-switch-only-using-the-default-code-block/#findComment-1543206 Share on other sites More sharing options...
taquitosensei Posted February 23, 2017 Share Posted February 23, 2017 you don't have a break after your default case. Not sure if that would cause this though. Quote Link to comment https://forums.phpfreaks.com/topic/303261-php-switch-only-using-the-default-code-block/#findComment-1543261 Share on other sites More sharing options...
Jacques1 Posted February 23, 2017 Share Posted February 23, 2017 No. A break would be entirely redundant. Quote Link to comment https://forums.phpfreaks.com/topic/303261-php-switch-only-using-the-default-code-block/#findComment-1543262 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.