0

I have 2 string :

$data1 = "8,11,";
$data2 = "2,3,";

and I do this :

$stuff = explode(",", $data1, -1);
$amount = explode(",", $data2, -1);

So the array like this :

$stuff have an Array ( [0] => 8 [1] => 11 ) 
$amount have an Array ( [0] => 2 [1] => 3 )

and then do foreach like this :

foreach($stuff as $index => $value){

    $query= "SELECT * FROM products WHERE id = ?";
    $STH2 = $DBH->prepare($query);
    $STH2->execute(array($value['0']));

    while($Products_all = $STH2->fetch()){
and so on....
.......

What I want to do is to print product id 8 and id 11. In fact, it get product id 8 and id 1. What's wrong with my code? Why it has product id 1 not 11? Thank you.

additional question: why it has to change to "$value" ?

Azhar
  • 329
  • 1
  • 4
  • 17

3 Answers3

1

Replace

 $STH2->execute(array($value['0']));

to

 $STH2->execute(array($value));
Bhumi Shah
  • 9,323
  • 7
  • 63
  • 104
0

You put into SQL query only first char of value ($value[0]) instead of full value.

$STH2->execute(array($value));

The second thing is, why you use queries in FOR loop insterad of one query?

SELECT ...
FROM ...
WHERE id IN (8, 11)
pavel
  • 26,538
  • 10
  • 45
  • 61
  • Because I want to use both of the array (stuff and amount), using 'foreach'. – Azhar Nov 10 '14 at 13:05
  • OK, no problem. With 3 queries there is no problem, with hundreds it will be worse and slower. The solution is in my answer, remove [0] – pavel Nov 10 '14 at 13:07
0

Ohhh, sry change this line:

$STH2->execute(array($value['0']));

to

$STH2->execute(array($value));
Ashique C M
  • 733
  • 4
  • 8