duanbushi1479 2013-05-26 21:21
浏览 52
已采纳

如何基于数组执行MySql'id'查找并将结果保存在新数组中

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?

  • 写回答

1条回答 默认 最新

  • dsasd12314 2013-05-26 21:28
    关注

    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'];
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退