1

我在数据库中有一个 user_privilege 列,如下所示:

username        user_privilege 

a                  7gz,agt_courses,newbill

b                 dataDisplay,previllige,newUsers

c                 newbill,dataDisplay,previllige

我想搜索我OR在每个之间使用的 user_privilege 列中的每个值,FIND_IN_SET但我的工作不正确?

我写了php代码:

ob_start();
session_start();        

    $username = $_POST['username'];
            $username2 = $_POST['username2'];
    $finduser = mysqli_query($link,"SELECT * FROM LOGIN 
        WHERE username='".$username."' AND username2='".$username2."' 
        AND FIND_IN_SET('7gz', user_previllige) OR FIND_IN_SET('agt_courses',user_previllige) OR FIND_IN_SET('newbill',user_previllige) OR FIND_IN_SET('new_Trainee',user_previllige)OR FIND_IN_SET('dataDisplay',user_previllige) OR FIND_IN_SET ('previllige',user_previllige) OR FIND_IN_SET('newUsers',user_previllige) OR FIND_IN_SET('Customers ',user_previllige )") 
        or die("error");

if(mysqli_num_rows($finduser) !=0)
        {

            while($row = mysqli_fetch_array($finduser))
                {$uname = $row['username'];
                $uname2 = $row['username2'];
                $arr=explode(",",$row['user_previllige']);
                //if($arr=='dataDisplay'){echo 'yes';}//elseif($arr=='new_Trainee'){echo 'yes';}
                }

        }

        if($username == $uname && $username2 == $uname2)
            {$_SESSION['sessionname'] =$uname;
            $_SESSION['sessionname2'] =$uname2;
            $_SESSION['sessionpre'] =$arr;
            header ("location:../agtSite/agt2 - Copy.php");
            //echo '<meta http-equiv="refresh" content="1" url="../agtSite/agt2 - Copy.php"/>';
            }

            else echo 'اسم المستخدم غير موجود <a href="login.php"> اذهب للتسجيل مره اخري </a> ';

}
ob_end_flush();

但它不能正常工作有多重搜索的解决方案吗??????

4

2 回答 2

0

您的脚本中有错字。列名是user_privilegeuser_previllige在查询中使用的。

采用

$finduser = mysqli_query($link,"SELECT * FROM LOGIN 
        WHERE username='".$username."' AND username2='".$username2."' 
        AND FIND_IN_SET('7gz', user_privilege) OR FIND_IN_SET('agt_courses',user_privilege) OR FIND_IN_SET('newbill',user_privilege) OR FIND_IN_SET('new_Trainee',user_privilege)OR FIND_IN_SET('dataDisplay',user_privilege) OR FIND_IN_SET ('previllige',user_privilege) OR FIND_IN_SET('newUsers',user_privilege) OR FIND_IN_SET('Customers ',user_privilege)") 
        or die("error");
于 2013-10-19T12:30:58.883 回答
0

看看这个查询:

SELECT
    *
FROM
    LOGIN
WHERE
    username = 'a'
    AND 
    (
            FIND_IN_SET( '7gz' ,user_privilege )
        OR  FIND_IN_SET( 'agt_courses' ,user_privilege )
        OR  FIND_IN_SET( 'newbill' ,user_privilege )
        OR  FIND_IN_SET( 'new_Trainee' ,user_privilege )
        OR  FIND_IN_SET( 'dataDisplay' ,user_privilege )
        OR  FIND_IN_SET( 'previllige' ,user_privilege )
        OR  FIND_IN_SET( 'newUsers' ,user_privilege )
        OR  FIND_IN_SET( 'Customers' ,user_privilege )
    )

使用下面的测试表和测试数据进行测试。有效 :

--
-- Table structure for table `login`
--

CREATE TABLE IF NOT EXISTS `login` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(100) NOT NULL DEFAULT '',
  `user_privilege` varchar(100) NOT NULL DEFAULT '',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=4 ;

--
-- Dumping data for table `login`
--

INSERT INTO `login` (`id`, `username`, `user_privilege`) VALUES
(1, 'a', '7gz,agt_courses,newbill'),
(2, 'b', 'dataDisplay,previllige,newUsers'),
(3, 'c', 'newbill,dataDisplay,previllige');
于 2013-10-19T13:02:02.210 回答