-3

i want to execute a query with mysql pdo

the query is

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

see the executed screen below

enter image description here

i'm getting data in this format

    $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 Answers3

0

You can't pass an array o the bindValue() function, and you can't test to equality with multiple values in the query instead you need to use the in operator. Then you can implode the array to pass a string to the bindValue() function:

$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;
    } 
Samer Abu Gahgah
  • 751
  • 1
  • 9
  • 18
0

i got the solution referring this thread Can I bind an array to an IN() condition?

note FIND_IN_SET

$skills = array('html','css');
SELECT * FROM table WHERE FIND_IN_SET(id, :array)
$skills = implode(',', $skills); 
$stmt->bindParam('array', $skills);
-1

change code to :

  $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();
Farhang Negari
  • 229
  • 1
  • 12