doubipeng1336 2017-09-03 18:23
浏览 100
已采纳

PHP / PDO查询后大于或等于运算符[重复]

This question already has an answer here:

I'm currently implementing a follow system using PHP and PDO. The code determines if the button "Follow" or "Unfollow" should show based on the inserted data. The database is updating correctly but for some reason the greater than or equal to operator is not working properly, or I'm not querying the database properly. (Not sure which) Anyone know what I'm doing wrong?

In 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);
      }
}

In 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);
      }
}

And in index.php (where button appears):

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>";

                    }
                }
            }
</div>
  • 写回答

1条回答 默认 最新

  • doushi5752 2017-09-03 18:31
    关注

    Your trying to test the result set with an ID, $query2->fetch returns an associative array.

    You need to access the field in the result set...

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

    If you just want to say if a row has not been returned, it will return false.

    if($query2result !== false){
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 torch.multiprocessing.spawn.ProcessExitedException: process 1 terminated with signal SIGKILL
  • ¥15 QuartusⅡ15.0编译项目后,output_files中的.jdi、.sld、.sof不更新怎么解决
  • ¥15 pycharm输出和导师的一样,但是标红
  • ¥15 想问问富文本拿到的html怎么转成docx的
  • ¥15 我看了您的文章,遇到了个问题。
  • ¥15 GitHubssh虚拟机连接不上
  • ¥15 装完kali之后下载Google输入法 重启电脑后出现以下状况 且退不出去 桌面消失 反复重启没用
  • ¥15 ESP-IDP-BLE配网连接wifi
  • ¥15 ue2.6.12版本用的若以,安装gojs,引入import * as go from 'gojs';报错
  • ¥15 服务器上的网站安装php5.6版本