简体   繁体   中英

how to use mysql like operator inside if else condition in php

I written mysql like operator inside if else condition in php. But am not getting output, Can anyone tell me how to use it. I am giving my code below

   if ($role == "admin")
{
    $sql="SELECT * FROM registration";
}
elseif($role == 'M%')
{
    $sql="SELECT * FROM registration where reporting_manager='$role' OR role='$role'";
}
else
{
    $sql="SELECT * FROM registration WHERE role='$role'";
}

but its not taking the second condition, directly going to last. What the mistake i had done in this code, or how to write the like operator in if else condition. Thanks in advance

You are mixing mysql with php. use the following PHP:

elseif(strtoupper($role[0]) === 'M')

Try this one query;)

$sql = "SELECT *
      FROM registration
      WHERE ('$role' = 'admin')
      OR ('$role' = 'M%' AND (reporting_manager='$role' OR role='$role')) 
      OR (role='$role')";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM