PHP / PDO查询后大于或等于运算符

时间:2017-09-03 18:23:13

标签: php mysql pdo

我目前正在使用PHP和PDO实现一个跟随系统。代码确定是否应根据插入的数据显示“Follow”或“Unfollow”按钮。数据库正在正确更新但由于某种原因大于或等于运算符无法正常工作,或者我没有正确查询数据库。 (不知道哪个)任何人都知道我做错了什么?

在followon.php中:

if($row['userID']  && $row['userName']){
    if($row['userID']!=$user_id){
        $follow_userid = $row['userID'];

        $stmt = $user_follow->runQuery("SELECT id FROM following WHERE user1_id=':user_id' AND user2_id=':follow_userid'");
                $stmt->execute(array(":user_id"=>$user_id,":follow_userid"=>$follow_userid));
                $follow = $stmt->fetch(PDO::FETCH_ASSOC);

                if(!$follow >= 1){

            $stmt = $user_follow->runQuery("INSERT INTO following(user1_id, user2_id) VALUES (?, ?)");
                $stmt->bindValue(1,$user_id);
                $stmt->bindValue(2,$follow_userid);
                $stmt->execute();

            $stmt = $user_follow->runQuery("UPDATE tbl_users SET following = following + 1 WHERE userID = ?");
                $stmt->bindValue(1,$user_id);
                $stmt->execute();

            $stmt = $user_follow->runQuery("UPDATE tbl_users SET followers = followers + 1 WHERE userID = ?");
                $stmt->bindValue(1,$follow_userid);
                $stmt->execute();

                }

        header("Location: index.php?id=".$currentID);
      }
}

在followoff.php中:

if($row['userID']  && $row['userName']){
    if($row['userID']!=$user_id){
        $unfollow_userid = $row['userID'];

        $stmt = $user_unfollow->runQuery("SELECT id FROM following WHERE user1_id=':user_id' AND user2_id=':unfollow_userid'");
                $stmt->execute(array(":user_id"=>$user_id,":unfollow_userid"=>$unfollow_userid));
                $follow = $stmt->fetch(PDO::FETCH_ASSOC);

                if($follow >= 1){

            $stmt = $user_unfollow->runQuery("DELETE FROM following WHERE user1_id= ? AND user2_id= ?");
                $stmt->bindValue(1,$user_id);
                $stmt->bindValue(2,$unfollow_userid);
                $stmt->execute();

            $stmt = $user_unfollow->runQuery("UPDATE tbl_users SET following = following - 1 WHERE userID = ?");
                $stmt->bindValue(1,$user_id);
                $stmt->execute();

            $stmt = $user_unfollow->runQuery("UPDATE tbl_users SET followers = followers - 1 WHERE userID = ?");
                $stmt->bindValue(1,$unfollow_userid);
                $stmt->execute();

                }

        header("Location: index.php?id=".$currentID);
      }
}

在index.php中(按钮出现):

if($user_id){
                if($user_id!=$id){

                    $query2 = $user_home->runQuery("SELECT id FROM following WHERE user1_id=':user_id' AND user2_id=':id'");
                                        $query2->execute(array(":user_id"=>$user_id,":id"=>$id));
                                        $query2result = $query2->fetch(PDO::FETCH_ASSOC);

                    if($query2result >= 1){
                                                echo "<a href='followoff.php?id=$currentID' class='btn btn-default btn-xs'>Unfollow</a>";

                    }
                    else{

                        echo "<a href='followon.php?id=$currentID' class='btn btn-info btn-xs'>Follow</a>";

                    }
                }
            }

1 个答案:

答案 0 :(得分:1)

您尝试使用ID测试结果集,$query2->fetch会返回一个关联数组。

您需要访问结果集中的字段...

if($query2result['id'] >= 1){

如果您只是想说一行是否还没有返回,则会返回false

if($query2result !== false){