0

I have been looking at using IN(array) and I have seen a lot of examples here such as This example and this but for some reason my query does not return anything. I am making an android application for job advertising, I have a table with all the jobs in my database and all jobs have a typewhich can be any of these "Advert","Consultancy","Freelance","Full Time","Internship","Part Time","Temporary". On the android side user selects which job type/s they wish to view and I compile a JSONArray and convert it to a string using toString() then send it to the server side, the typical array looks like ["Advert","Consultancy","Freelance","Full Time","Internship","Part Time","Temporary"] When received its converted to a php array

 `$types = json_decode('["Advert","Consultancy","Freelance","Full Time","Internship","Part Time","Temporary"]', TRUE);`

then finally

$match_list = join(",", $types);
$result =  $this->conn->prepare("SELECT j.id, u.username, j.title, j.description,j.category, j.type, j.submissionDeadline, j.link, j.datecreated FROM job_tb j JOIN user_tb u ON u.id = j.userFk WHERE DATE(j.submissionDeadline) > DATE(NOW()) AND j.type IN (?) ORDER BY j.datecreated DESC");
$result->bind_param("s", $match_list);
$result->execute();
$response["jobs"] = array();

$result->bind_result($id, $username, $title, $description, $category, $type, $submissionDeadline, $link, $datecreated);
while($row = $result->fetch()) 
{
    $job = array();
    $job["id"] = $id;
    $job["username"] = $username;
    $job["title"] = $title;
    $job["description"] = $description;
    $job["category"] = $category;
    $job["type"] = $type;
    $job["submissionDeadline"] = $submissionDeadline;  
    $job["link"] = $link; 
    $job["dateCreated"] = $datecreated;   
    $response["jobs"][] =  $job;

}

Unfortunately nothing is returned but when I pass a single job type item array like ["Part Time"] it returns all jobs of type Part Time so what could be the problem?

Rando roxford
  • 141
  • 2
  • 10
  • It is all bound as 1 value. You need a placeholder for each value, and you then need to bind each value. – chris85 Oct 24 '17 at 13:25
  • 1
    Possible duplicate of [Can I bind an array to an IN() condition?](https://stackoverflow.com/questions/920353/can-i-bind-an-array-to-an-in-condition) – aynber Oct 24 '17 at 13:25
  • Post the result var_dump($match_list); here please so we can see what it contains. – Nic3500 Oct 24 '17 at 13:28
  • @Nic its `Advert,Consultancy,Freelance,Full Time,Internship,Part Time,Temporary` – Rando roxford Oct 24 '17 at 13:32
  • So the query becomes .... IN (Advert,Consultancy,Freelance,Full Time,Internship,Part Time,Temporary). You need to quote each item. Look at this https://phpdelusions.net/pdo#in there is an example of how to join + quote your array elements. – Nic3500 Oct 24 '17 at 13:35
  • or this: $inQuery = implode(',', array_fill(0, count($ids), '?')); from https://stackoverflow.com/questions/920353/can-i-bind-an-array-to-an-in-condition. You are very close! – Nic3500 Oct 24 '17 at 13:36
  • okay let me check it out – Rando roxford Oct 24 '17 at 13:36

0 Answers0