我想用mysql
pdo
query
是
SELECT * FROM `user_skills` WHERE skills in ('html','css')
请参阅下面执行的屏幕
我正在以这种格式获取数据
$skills = array('html','css');
$sql = "SELECT first_name,last_name,mobile_number,prev_comany_name,user_id FROM user_details WHERE skills=:input";
$stmt = $db->prepare($sql);
$stmt->bindValue("input", $skills);
$stmt->execute();
/** foreach($skills as $skill){
echo $skill;
} **/
答案 0 :(得分:0)
您无法通过bindValue()
函数传递数组,并且您无法在查询中测试与多个值的相等性,而是需要使用in
运算符。然后你可以内爆数组将一个字符串传递给bindValue()
函数:
$skills = array('html','css');
$sql = "SELECT first_name,last_name,mobile_number,prev_comany_name,user_id FROM user_details WHERE skills IN (:input)";
$stmt = $db->prepare($sql);
$stmt->bindValue(":input", implode(",", $skills), PDO::PARAM_STR);
$stmt->execute();
/** foreach($skills as $skill){
echo $skill;
}
答案 1 :(得分:0)
将代码更改为:
$skills = array('html','css');
$sql = "SELECT first_name,last_name,mobile_number,prev_comany_name,user_id FROM user_details WHERE skills IN (:input)";
$stmt = $db->prepare($sql);
$stmt->bindValue(":input", "'".implode("','", $skills)."'");
$stmt->execute();
答案 2 :(得分:0)
我得到的解决方案是thread
Can I bind an array to an IN() condition?
注意FIND_IN_SET
$skills = array('html','css');
SELECT * FROM table WHERE FIND_IN_SET(id, :array)
$skills = implode(',', $skills);
$stmt->bindParam('array', $skills);