Below is my database structure:
PostUUID subid pricing Name Poscode
abc1 1 100 userA 56000
abc2 2 100 userA 56000
abc3 1 100 userB 56000
abc4 2 100 userB 56000
abc5 1 100 userC 56000
abc6 2 100 userC 56000
Now I want to perform search on this table called 'posts'.
Say I want look for PostUUID(field) where the details are as below:
**-subid =1,2
-postcode=56000
-pricing-100**
In the above database image, there are 3 users meeting these criteria, namely vani,kalni and nair.
So the result should return 6 rows with unique PostUUID.
My query:
$a=$data['sub'];
foreach($a as $v=>$k)
{
$key[]=$v;
}
//variables
$key2=implode(',',$key);
$postcode=$data['postcode'];
$rate=$data['rate'];
$statement = $pdo->prepare("SELECT * FROM posts,subjects WHERE posts.subid IN (:key2) AND posts.pricing=:rate2 AND posts.Poscode=:postcode2 AND subjects.subid=posts.subid ORDER BY posts.Poscode DESC LIMIT 60");
$statement->execute(array(':key2' => $key2,':rate2'=>$rate,':postcode2'=>$postcode));
$json = array();
while( $row = $statement->fetch()) {
// array_push($json, array($row['Name'], $row['PostUUID']));
array_push($json, array("name" => $row['Name'], "id" => $row['PostUUID'],"rate" => $row['pricing'],"postcode" => $row['Poscode'],"subject" => $row['subname'],"reputation" => $row['Reputation'],"plus" => $row['ReviewPlus'],"neg" => $row['ReviewNeg'],"weekM" => $row['week_morning'],"weekA" => $row['week_afternoon'],"weekE" => $row['week_evening'],"endM" => $row['weekend_morning'],"endA" => $row['weekend_afternoon'],"endE" => $row['weekend_evening']));
}
header('Content-Type: application/json');
echo json_encode($json);
The result shown, 6 rows with same PostUUID. It simply iterated twice for each user as each user has two matching records for the above search criteria thus repeats same data.
Can anyone tell me what's wrong in my query please?