php switch only using the default code block

Codes here !

Moderators: egami, macek, gesf

Post Reply
alanwaston
New php-forum User
New php-forum User
Posts: 4
Joined: Sat Dec 03, 2016 1:32 am

Wed Feb 22, 2017 8:26 pm

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:

Code: Select all

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

} 
    }  
________________________________________________________
Relax: ringtones, iphone 6 ringtone
chorn
php-forum GURU
php-forum GURU
Posts: 651
Joined: Fri Apr 01, 2016 2:18 am

Wed Feb 22, 2017 11:15 pm

can not confirm

https://3v4l.org/1GGB1
User avatar
dinoroger
New php-forum User
New php-forum User
Posts: 12
Joined: Mon Feb 20, 2017 1:59 pm

Fri Feb 24, 2017 7:13 am

Try changing
switch ($cname)
to
switch (trim(strtoupper($cname)))

You never know when your value can comes in with a hanging space or wrong case. Then as you are already doing always case for the upper case value.
Post Reply