0

I have an array family like -

Array ( [0] => 2510-24 [1] => 2510-48 [2] => 2510G [3] => 2520 [4] => 2520G [5] => 2530ya [6] => 2530ya3 [7] => 2530yb [8] => 2610 [9] => 2615 [10] => 2620 [11] => 2626 [12] => 2650 [13] => 2810 [14] => 2900 [15] => 2910 [16] => 2915 [17] => 2920 [18] => 3500 [19] => 3500jr [20] => 3500strk [21] => 3800 [22] => 3800FIPS [23] => 4100 [24] => 4200 [25] => 5300 [26] => 5400 [27] => 5400FIPS [28] => 5400R [29] => 5400RFIPS [30] => 5900 [31] => 6100 [32] => 6200 [33] => 6600 [34] => 8200 [35] => 8200FIPS [45] => Mako [46] => MSM317 [47] => MSM430 [48] => MSM460 [49] => MSM466 [50] => MSM720 [51] => MSM760 [52] => MSM765 [53] => MSM775 [54] => stack-AR [55] => stack-TA ) 

Now I have a db query in which I want to query my database only for those rows where family is one of the above.

I have tired -

$status = 'passed';
$allTest = $conn->prepare('SELECT SUM( IF( STATUS = :status, 1, 0 ) ) passed_count FROM tooldata WHERE family IN (".$family.") GROUP BY family ORDER BY family' );
$allTest->execute(array(':status' => $status));

and even implode -

$family = implode(',',$family);

// Finding passed test cases on each platform   

    $status = 'passed';
    $allTest = $conn->prepare('SELECT SUM( IF( STATUS = :status, 1, 0 ) ) passed_count FROM tooldata WHERE family IN ('.$family.') GROUP BY family ORDER BY family' );
    $allTest->execute(array(':status' => $status));

But this does not work. What is wrong here ? What is the better way to do the same ?

user3610827
  • 33
  • 1
  • 7

1 Answers1

-2
$imploded_family = implode(',', array_map([$conn, 'quote'], $family));
$allTest = $conn->prepare('SELECT SUM( IF( STATUS = :status, 1, 0 ) ) passed_count FROM tooldata WHERE family IN ('.$imploded_family.') GROUP BY family ORDER BY family' );
JimiDini
  • 2,039
  • 12
  • 19
  • -1 for putting actual data inside the sql query – ThiefMaster May 13 '14 at 07:07
  • @ThiefMaster Actually, it gets more unruly to have normal arguments mixed with array-type ones; doing it this way isn't necessarily bad imo. – Ja͢ck May 13 '14 at 07:08
  • @ThiefMaster PDO doesn't allow to use placeholders in a meaningful way in such queries. Creating prepared query with arbitrary number of arguments doesn't sound like a way to go either – JimiDini May 13 '14 at 08:25
  • Bah. Anyway, if you edit your post to mention this I'll remove the (currently locked-in) downvote. – ThiefMaster May 13 '14 at 16:38