I have an array with a lot (100-800) of id's. I want to query my database doing a lookup on the id's from my array with any matches from the database and then save the matching results in a new array.
My array:
//print_r($_POST["fb_friend_uid"])
Array
(
[0] => Array
(
[id] => 263901486
)
[1] => Array
(
[id] => 502533736
)
)
--1. Then I try to see if any values from the array matches with records in the database - is this the correct way to do this?:
$ids = join(',',$_POST["fb_friend_uid"]);
SELECT *
FROM vote WHERE
vote_fb_uid IN ($ids)
AND DATE(FROM_UNIXTIME(vote_time)) = DATE(NOW())";
--2. Then im struggling to save the matching records in a new array - this is what ive tried with:
while($row=mysqli_fetch_assoc($result)){
$savedResultsInNewArray=[$row['vote_fb_uid']];
}
Im looking to return the new array in a format of: echo json_encode($savedResultsInNewArray);
Im a bit unsure the above is the right way to do that?
The first part is to get your ID's from the array. You can do this with a loop:
<?php
foreach ($_POST["fb_friend_uid"] as $uid) {
$uids[] = $uid['id'];
}
$ids = join(',', $uids);
/*
* Query is correct
*
* SELECT *
* FROM vote WHERE
* vote_fb_uid IN ($ids)
* AND DATE(FROM_UNIXTIME(vote_time)) = DATE(NOW())";
*/
// For the returning part.
while($row=mysqli_fetch_assoc($result)){
$savedResultsInNewArray[] = $row['vote_fb_uid'];
}