dongtai6741 2017-05-21 02:39
浏览 43
已采纳

从用户ID的数组中选择帖子ID - mysql

I have an Array of id's I follow if I echo:

echo json_encode($followingIds);

prints:

[{"User_two_id":"3"},{"User_two_id":"4"},{"User_two_id":"5"}]

I now want to select All id's from Post table where the value/s of Author are equal to the value of my User_two_id's in my array.

"Author is indexing id's of user_two"

Here is where I've got so far which is not working

function getPosts( $follwingIds ) {

        $sql = "SELECT id
        FROM posts
        WHERE Author IN ('$follwingIds')
        GROUP BY id
        HAVING COUNT(DISTINCT Author)";

    $result = $this->conn->query( $sql );

    if ( $result != null ) {

        while ( $row = $result->fetch_assoc() ) {
            $returnArray[] = $row;
        }
    }




}
  • 写回答

1条回答 默认 最新

  • douchai7891 2017-05-21 02:46
    关注

    One issue is here:

    WHERE Author IN ('$follwingIds')
    

    here $follwingIds in an JSON object where as mysql IN required value in comma separated values. So instead of making an json array convert it to string like:

    $follwIds = implode(',', array_column($follwingIds, 'User_two_id'));
    

    and use it in query like:

    WHERE Author IN ($follwIds)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 用51单片机控制急停。
  • ¥15 孟德尔随机化结果不一致
  • ¥15 在使用pyecharts时出现问题
  • ¥15 深度学习残差模块模型
  • ¥50 怎么判断同步时序逻辑电路和异步时序逻辑电路
  • ¥15 差动电流二次谐波的含量Matlab计算
  • ¥15 Can/caned 总线错误问题,错误显示控制器要发1,结果总线检测到0
  • ¥15 C#如何调用串口数据
  • ¥15 MATLAB与单片机串口通信
  • ¥15 L76k模块的GPS的使用