rowCount()始终返回0

问题描述 投票:-2回答:1

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

    ?>
php pdo
1个回答
0
投票

我决定改变我的代码,从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\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.");
    }
© www.soinside.com 2019 - 2024. All rights reserved.