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

报告相同问题?

悬赏问题

  • ¥15 c语言数据结构实验单链表的删除
  • ¥15 关于#lua#的问题,请各位专家解答!
  • ¥15 什么设备可以研究OFDM的60GHz毫米波信道模型
  • ¥15 不知道是该怎么引用多个函数片段
  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline