duanchun5520 2018-03-06 05:05 采纳率: 0%
浏览 169
已采纳

rowCount()始终返回0

I have a problem with my code but I cannot solve it, I tried multiple things, but they did not work. Please if you can help me, tell me why rowCount() is always returning 0 even if I have a user with that name.

<?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条回答 默认 最新

  • du27271 2018-03-06 17:14
    关注

    I decided to change my code, from PDO -> MySqli and it's working !

    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.");
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥66 比特币地址如何生成taproot地址
  • ¥20 数学建模数学建模需要
  • ¥15 关于#lua#的问题,请各位专家解答!
  • ¥15 什么设备可以研究OFDM的60GHz毫米波信道模型
  • ¥15 不知道是该怎么引用多个函数片段
  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决