关于mysqli的问题?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (82)

所以我有一个名为follow_sysidfollower和的表following,并且我想显示我关注的人,但它不起作用

$sqlFollowing = "SELECT * from follows_syds t1, follows_syds t2
    where
    t1.follower = ?
    and
    t1.following != ?
    and
    t2.follower != ?
    AND
    t2.following != ?";

$Following       = $conn -> prepare($sqlFollowing);
$Following       -> bind_param('ssss',$metUser,$metUser,$metUser,$metUser);
$Following       -> execute();
$FollowingGET    = $Following -> get_result();
$FollowingRows   = $FollowingGET -> num_rows;

ive尝试过类似的东西,但结果仍然不准确

$sqlFollowing      = "SELECT * from follows_syds t1
                                 INNER join follows_syds t2 on
                                 t1.follower = t2.following
                                 where
                                 t2.follower = ?
                                 and
                                 t1.following = ?
                                 group by t2.id";
$Following         = $conn -> prepare($sqlFollowing);
$Following        -> bind_param('ss',$getUser,$getUser);
$Following        -> execute();
$FollowingGET      = $Following -> get_result();
$FollowingRows     = $FollowingGET -> num_rows;

while($b = $FollowingGET -> fetch_assoc()){
$ve = $b['following'];

}
  $sqlowing      = "SELECT * from follows_syds t1
                    INNER join follows_syds t2 on
                    t1.follower = t1.follower
                    where
                    t2.follower = ?
                    and
                    t2.following != ?
                    AND
                    t1.follower != ?
                    and
                    t1.following != ?
                    group by t2.id";
  $owing         = $conn -> prepare($sqlowing);
  $owing        -> bind_param('ssss',$getUser,$ve,$ve,$getUser);
  $owing        -> execute();
  $owingGET      = $owing -> get_result();
  $owingRows     = $owingGET -> num_rows;
  while($a = $owingGET -> fetch_assoc()){
      print_r($a );
      echo "</br>";
}
提问于
用户回答回答于

你必须按名称创建表 follower_following

CREATE TABLE `follower_following` (
  `id` int(11) NOT NULL,
  `follower_id` int(11) NOT NULL,
  `following_id` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

ALTER TABLE `follower_following`
  ADD PRIMARY KEY (`id`);

ALTER TABLE `follower_following`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
COMMIT;

作为关注者的用户的ID必须保存follower_id在此表的列中。并且该用户的id必须保存following_id在此表的列中。如果你想获得id = 1的任何用户的关注者,必须运行此查询。

function get_username($id){
    $sql = "SELECT * FROM user where id = $id";
    $result = $conn->query($sql);
    foreace($result as $row){
      return $row['username'];
    }
}
$sql = "SELECT * FROM follower_following where following_id = 1";
$result = $conn->query($sql);
foreace($result2 as $row2){
    echo get_username($row2['follower_id']);
}

扫码关注云+社区

领取腾讯云代金券