rowCount()始终返回0

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了rowCount()始终返回0相关的知识,希望对你有一定的参考价值。

我的代码有问题但是我无法解决它,我尝试了多种方法,但是它们没有用。如果你可以帮助我,请告诉我为什么rowCount()总是返回0,即使我有一个具有该名称的用户。

<?php
    include("../api/config.php");

    if(isset($_GET['user'],$_GET['key']) and !empty($_GET['user']) and !empty($_GET['key']))
    {
        $user = htmlspecialchars(urldecode($_GET['user']));
        $key = htmlspecialchars($_GET['key']);
        $bdd = new PDO('mysql: host = '.$dbhost.';dbname = '.$dbname.'', $dbusername, $dbpassword);
        $requser = $bdd->prepare("SELECT * FROM users WHERE username = ? AND confirmkey = ?");
        $requser->execute(array($user, $key));
        $userexist = $requser->rowCount();

        if($userexist==1)
        {
            $ufetch = $requser->fetch();
            if($ufetch['isactive']==0)
            {
                $confirm_account = $bdd->prepare("UPDATE users SET isactive = 1 WHERE username = ? AND confirmkey = ?");
                $confirm_account->execute(array($user, $key));
                die('Congratulations! Your account was successfully confirmed. <a href="../?page=login">Login</a>');
                sleep(3);
                header("Location: ../?page=login");

                $bdd = null;
                $requser = null;
                $confirm_account = null;
            }
            else
            {
                echo "This account has already been confirmed.";
                $bdd = null;
                $requser = null;
            }
        }
        else
        {
            echo "User does not exist" . $userexist;
            $bdd = null;
            $requser = null;
        }
    }
    else 
    {
        die("Confirmation key or username is incorrect.");
    }

    ?>
答案

我决定改变我的代码,从PDO - > MySqli,它正在工作!

include("../api/config.php");

    if(isset($_GET['user'],$_GET['key']) and !empty($_GET['user']) and !empty($_GET['key']))
    {
        $mysqli = new mysqli($dbhost, $dbusername, $dbpassword, $dbname);/* check connection */
        if (mysqli_connect_errno()) {
            printf("Connect failed: %s
", mysqli_connect_error());
            exit();
        }

        $user = htmlspecialchars(urldecode($_GET['user']));
        $key = htmlspecialchars($_GET['key']);

        if ($result = $mysqli->query("SELECT *  FROM users WHERE username = '$user'")) {
            $row_cnt = $result->num_rows;
            $userexist = $row_cnt;
        }

        //test

        //$bdd = new PDO('mysql: host = '.$dbhost.';dbname = '.$dbname.'', $dbusername, $dbpassword);
        //$requser = $bdd->prepare("SELECT * FROM users WHERE username = :name");
        //$requser->execute(array(":name"=>$user));
        //$row = $requser->fetch(PDO::FETCH_ASSOC);
        //$userexist = $requser->rowCount();

        if($userexist==1)
        {
            $ufetch = mysqli_fetch_object($result);
            if($ufetch->isactive == 0)
            {
                $confirm_account = $mysqli->query("UPDATE users SET isactive = 1 WHERE username = '$user' AND confirmkey = '$key'");
                die('Congratulations! Your account was successfully confirmed. <a href="../?page=login">Login</a>');
                sleep(3);
                header("Location: ../?page=login");

                $mysqli->close();
                $result->close();
                $confirm_account->close();
            }
            else
            {
                echo "This account has already been confirmed.";
                $mysqli->close();
                $result->close();
            }
        }
        else
        {
            echo "User does not exist" . $userexist;
            $mysqli->close();
            $result->close();
        }
    }
    else 
    {
        die("Confirmation key or username is incorrect.");
    }

以上是关于rowCount()始终返回0的主要内容,如果未能解决你的问题,请参考以下文章

Android SMS Verification API 结果码始终为 0

PHP PDO Update语句总是返回0行受影响的rowCount()

Oracle sql%rowcount 返回影响行数;sql server @@RowCount返回影响行数

pyqt QFileSystemModel rowCount

嵌套片段数据在中继中始终相同

LiveData 分页列表大小始终为 0