rowCount()始终返回0

时间:2018-03-06 05:05:55

标签: php pdo

我的代码有问题,但我无法解决,我尝试了多种方法,但是它们没有用。如果你可以帮助我,请告诉我为什么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.");
    }

    ?>

1 个答案:

答案 0 :(得分:0)

我决定从PDO更改我的代码 - &gt; 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\n", 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.");
    }