如何用mysql pdo执行查询

时间:2017-10-10 10:46:34

标签: php mysql pdo

我想用mysql pdo

执行查询

query

SELECT * FROM `user_skills` WHERE skills in ('html','css')

请参阅下面执行的屏幕

enter image description here

我正在以这种格式获取数据

    $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;
    } **/

3 个答案:

答案 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);