PHP mysql使用FIND IN SET来搜索列值

时间:2016-06-16 04:59:05

标签: php mysql

这是我的数据库表“课程”

enter image description here 当我添加多个年龄组和国家/地区的数据时,我想检查它是否已经存在于db

示例名称不能与同一国家+年龄组合重复

如果name =“sw”,则用户无法再次为2岁时添加国家1或4或5或3

修改

$age1 = explode(",", $age);
$country1 = explode(",", $country);

foreach($age1 as $ages)
{
    foreach($country1 as $country2)
    {
        $where = "find_in_set( $country2,`country`) <> 0 AND find_in_set( $ages,`age`) <> 0 AND `name` = '" . $name . "'";
    }

}

请检查以下结果......

 var_dump($age);exit;

string '2,1' (length=3)

var_dump($country);exit;

string '1,4,2,3' (length=7)

 var_dump$country1

array (size=2)
  0 => string '2' (length=1)
  1 => string '1' (length=1)

的var_dump($ AGE-1);出口;

array (size=4)
  0 => string '1' (length=1)
  1 => string '4' (length=1)
  2 => string '2' (length=1)
  3 => string '3' (length=1)

1 个答案:

答案 0 :(得分:0)

这里你使用了两次相同变量的$,这里没有使用它。

$where = $country." IN  country AND ".$age." IN age";
$this->db->select("music_lessons","*",$where);
$res = $this->db->getResult();

返回结果行数