简体   繁体   中英

MySQLi Num Rows Doesnt Work

i have a simple login system and i get nothing when trying to fetch the number of rows, the same method used to work all the time, i dont know what is going on today.

Code:

<?php
class LoginClass {
    public $User;
    public $Pass;
    public $Query;
    function Init() {
        $User = $this->User;
        $Pass = $this->Pass;
        if($User != '')
        {
            if($Pass != '')
            {
                $this->HashPass();
            }
            else
            {
                echo 'Please Enter A Password.';
            }
        }
        else
        {
            echo 'Please Enter A Username or E-Mail.';
        }
    }

    function HashPass() {
        $Pass = $this->Pass;
        $this->Pass = hash('sha256', $Pass);
        $this->CheckUser();
    }

    function CheckUser() {
        $User = $this->User;
        if(!filter_var($User, FILTER_VALIDATE_EMAIL))
        {
            $this->Query = 'SELECT * FROM Users WHERE User = "'.$User.'" AND Pass = "'.$this->Pass.'"';
        }
        else
        {
            $this->Query = 'SELECT * FROM Users WHERE EMail = "'.$User.'" AND Pass = "'.$this->Pass.'"';
        }
        $this->CheckDB();
    }

    function CheckDB() {
        $Query = $this->Query;
        $Connect = new mysqli("127.0.0.1", "root", "", "Data");
        $Stmt = $Connect->prepare($Query)
        $Stmt->execute();
        $Stmt->store_result();
        echo $Stmt->num_rows;
        $Stmt->close();
        $Connect->close();
    }

    function SetupSession() {
        echo 'Test';
    }
}

the Check DB is the problem here and im able to echo out the query variable in that function everything is fine, here is exactly what i get

SELECT * FROM Users WHERE User = "Test" AND Pass = "532eaabd9574880dbf76b9b8cc00832c20a6ec113d682299550d7a6e0f345e25"

I also checked my DB and all my tables are setup correctly and there is no password.

OK, need more space than the comments area, the issue is clearly in this block:

function CheckDB() {
    $Query = $this->Query;
    $Connect = new mysqli("127.0.0.1", "root", "", "Data");
    $Stmt = $Connect->prepare($Query)
    $Stmt->execute();
    $Stmt->store_result();
    echo $Stmt->num_rows;
    $Stmt->close();
    $Connect->close();
}

I think it's because you aren't binding parameters to the prepared statement, you've already included them inline in your earlier statement. Therefore, you probably want to:

Switch to non-prepared statement

The easy option here will be to switch to a non-prepared statement. Replace your block with:

function CheckDB() {
    $Query = $this->Query;
    $Connect = new mysqli("127.0.0.1", "root", "", "Data");
    $Stmt = $Connect->query($Query)
    echo $Stmt->num_rows;
    $Stmt->close();
    $Connect->close();
}

A word of caution with this approach: you need to sanitize your user input in the block which defines $User , otherwise you're leaving yourself open to mysql injection. In that block, change this line:

$User = $this->User;

To the following:

$User = mysql_real_escape_string($this->User);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM