Azhar
Azhar

Reputation: 336

explode and then foreach data not the same (php error?)

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" ?

Upvotes: 0

Views: 101

Answers (3)

Ashique C M
Ashique C M

Reputation: 683

Ohhh, sry change this line:

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

to

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

Upvotes: 0

pavel
pavel

Reputation: 27092

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)

Upvotes: 0

Bhumi Shah
Bhumi Shah

Reputation: 9476

Replace

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

to

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

Upvotes: 1

Related Questions