简体   繁体   English

类函数不使用 mysqli 准备好的语句打印出 php 中的数据

[英]Class function not printing out data in php with mysqli prepared statement

I need some help on a function that is not echoing the $bonusjson array.我需要有关不回显$bonusjson数组的函数的一些帮助。 The function is CheckBonus() .该函数是CheckBonus() All of the other functions work fine, except this one.除了这个功能之外,所有其他功能都可以正常工作。 When it's ran it echos nothing and I cannot figure out why.当它运行时,它什么也没有回响,我不知道为什么。 This is really my first time working with classes and functions.这确实是我第一次使用类和函数。 The CheckBonus() is suppose to query the database and check the table to see if the $hash variable data exists, then run the query and print out the data. CheckBonus()假设查询数据库并检查表以查看$hash变量数据是否存在,然后运行查询并打印出数据。

I think the issue is with if($stmt->num_rows != 0) .我认为问题在于if($stmt->num_rows != 0) I have also tried using if(!empty($stmt->store_result())) instead and that prints the data, but if I change the $hash to something that is not in the database, it does not run AddUser() .我也尝试过使用if(!empty($stmt->store_result()))来打印数据,但是如果我将$hash更改为不在数据库中的内容,它不会运行AddUser()

<?

//Mysql connect info
$servername = "removed";
$username = "removed";
$password = "removed";
$database = "removed";

$link = new mysqli($servername, $username, $password, $database);
if ($link->connect_error)
{
    die("Connection failed: " . $link->connect_error);
}

//Get or send the information from/to the game
$func = $_GET['func'];
$hash = $link->real_escape_string($_GET['user']);
$bonus = $link->real_escape_string($_GET['bonus']);

class CheckUser
{
    function CheckBonus($hash)
    {
        global $link;
        global $hash;
        $query = "SELECT bonustier, resettime, userid FROM members WHERE userid = ?";
        if ($stmt = $link->prepare($query))
        {
            $stmt->bind_param('s', $hash);
            $stmt->execute();

            if($stmt->num_rows != 0) {
                $stmt->store_result();
                $stmt->bind_result($bonustier, $resettime, $hash);

                while($stmt->fetch())
                {
                    //Check if enough time has passed since last login bonus
                    $time = time();
                    $time2 = ((($time - $resettime) / 60) / 60);
                    $time3 = substr($time2, 0, strpos($time2, "."));

                    if($time3 >= 24)
                    {
                        $connection = 1;
                        $usergood = 1;
                        $loginbonus = 1;
                        $hoursince = $time3;

                        $bonusarray = array(
                            'BonusCheck' => array(
                                'connection' => $connection,
                                'usergood' => $usergood,
                                'loginbonus' => $loginbonus,
                                'bonustier' => $bonustier,
                                'hoursince' => $time3
                                ),
                            );

                        $bonusjson = json_encode($bonusarray, 128);
                        echo $bonusjson;
                    }
                    else //If enough time has not passed, tell the game.
                    {
                        $connection = 1;
                        $usergood = 1;
                        $loginbonus = 0;
                        $hoursince = $time3;

                        $bonusarray = array(
                            'BonusCheck' => array(
                                'connection' => $connection,
                                'usergood' => $usergood,
                                'loginbonus' => $loginbonus,
                                'bonustier' => $bonustier,
                                'hoursince' => $time3
                                ),
                            );

                        $bonusjson = json_encode($bonusarray, 128);
                        echo $bonusjson;
                    }
                }
            }
            else
            {
                $this->AddUser($hash);
            }

        }
        $stmt->close();
    }

    function UpdateBonus($hash, $bonus)
    {
        global $link;
        global $hash;
        global $bonus;
        if($stmt = $link->prepare("UPDATE members SET loginbonus = ?, bonustier = ?, resettime = ? WHERE userid=?"))
        {
            $stmt->bind_param('ssss', $loginbonus, $bonustier, $resettime, $hash);

            switch($bonus)
            {
                case 0:
                    $bonustier = 1;
                    break;
                case 1:
                    $bonustier = 2;
                    break;
                case 2:
                    $bonustier = 3;
                    break;
                case 3:
                    $bonustier = 4;
                    break;
                case 4:
                    $bonustier = 5;
                    break;
                case 5:
                    $bonustier = 0;
                    break;
            }

            $loginbonus = 0;
            $resettime = time();
            $success = '1';

            $updatearray = array(
                'UpdateBonus' => array(
                    'login' => $loginbonus,
                    'result' => $success
                    ),
                );

            $updatejson = json_encode($updatearray, 128);
            echo $updatejson;

            $stmt->execute();
            $stmt->close();
        }
    }

    function AddUser($hash)
    {
        global $link;
        global $hash;
        if($stmt = $link->prepare("INSERT INTO members (userid, loginbonus, bonustier, resettime) VALUES (?, ?, ?, ?)"))
        {
            $stmt->bind_param('ssss', $userid, $loginbonus, $bonustier, $resettime);

            $userid = $hash;
            $loginbonus = 1;
            $bonustier = 1;
            $resettime = time();

            $stmt->execute();
            $stmt->close();

            $this->CheckBonus($hash);
        }
    }

    function CheckFunc($func)
    {
        switch($func)
        {
            case cb:
                $this->CheckBonus($hash);
                break;
            case ub:
                $this->UpdateBonus($hash, $bonus);
                break;
            case au:
                $this->AddUser($hash);
                break;
            default:
                echo 'Function not found.';
                break;
        }
    }
}

$newObject = new CheckUser();
$newObject->CheckFunc($func);

?>

If you happen to downvote my quiestion, could you let me know why?如果你碰巧对我的问题投反对票,你能告诉我为什么吗? What am I doing wrong here, what could I do better and what could I work more on when asking a question?我在这里做错了什么,我可以做得更好,在提问时我可以做更多的工作吗?

I believe I figured out what I had done wrong.我相信我已经弄清楚我做错了什么。 It seems to work as intended now.它现在似乎按预期工作。 I moved $stmt->store_result();我移动了$stmt->store_result(); above the if($stmt->num_rows != 0) line and I changed that line to if($stmt->num_rows > 0) .if($stmt->num_rows != 0)行上方,我将该行更改为if($stmt->num_rows > 0) Not sure if the second change actually had any effect, but it seemed better to use > over != in the statement.不确定第二个更改是否真的有任何效果,但在语句中使用 > over != 似乎更好。

Here is the code in a working state.这是处于工作状态的代码。

//Mysql connect info
$servername = "removed";
$username = "removed";
$password = "removed";
$database = "removed";

$link = new mysqli($servername, $username, $password, $database);
if ($link->connect_error)
{
    die("Connection failed: " . $link->connect_error);
}

//Get or send the information from/to the game
$func = $_GET['func'];
$hash = $link->real_escape_string($_GET['user']);
$bonus = $link->real_escape_string($_GET['bonus']);

class CheckUser
{
    function CheckBonus($hash)
    {
        global $link;
        global $hash;
        $query = "SELECT bonustier, resettime, userid FROM members WHERE userid = ?";

        if ($stmt = $link->prepare($query))
        {
            $stmt->bind_param('s', $hash);
            $stmt->execute();
            $stmt->store_result();

            if($stmt->num_rows > 0)
            {
                $stmt->bind_result($bonustier, $resettime, $hash);

                while($stmt->fetch())
                {
                    //Check if enough time has passed since last login bonus
                    $time = time();
                    $time2 = ((($time - $resettime) / 60) / 60);
                    $time3 = substr($time2, 0, strpos($time2, "."));

                    if($time3 >= 24)
                    {
                        $connection = 1;
                        $usergood = 1;
                        $loginbonus = 1;
                        $hoursince = $time3;

                        $bonusarray = array(
                            'BonusCheck' => array(
                                'connection' => $connection,
                                'usergood' => $usergood,
                                'loginbonus' => $loginbonus,
                                'bonustier' => $bonustier,
                                'hoursince' => $time3
                                ),
                            );

                        $bonusjson = json_encode($bonusarray, 128);
                        echo $bonusjson;
                    }
                    else //If enough time has not passed, tell the game.
                    {
                        $connection = 1;
                        $usergood = 1;
                        $loginbonus = 0;
                        $hoursince = $time3;

                        $bonusarray = array(
                            'BonusCheck' => array(
                                'connection' => $connection,
                                'usergood' => $usergood,
                                'loginbonus' => $loginbonus,
                                'bonustier' => $bonustier,
                                'hoursince' => $time3
                                ),
                            );

                        $bonusjson = json_encode($bonusarray, 128);
                        echo $bonusjson;
                    }
                }
            }
            else
            {
                $this->AddUser($hash);
            }

        }
        $stmt->close();
    }

    function UpdateBonus($hash, $bonus)
    {
        global $link;
        global $hash;
        global $bonus;

        if($stmt = $link->prepare("UPDATE members SET loginbonus = ?, bonustier = ?, resettime = ? WHERE userid=?"))
        {
            $stmt->bind_param('ssss', $loginbonus, $bonustier, $resettime, $hash);

            switch($bonus)
            {
                case 0:
                    $bonustier = 1;
                    break;
                case 1:
                    $bonustier = 2;
                    break;
                case 2:
                    $bonustier = 3;
                    break;
                case 3:
                    $bonustier = 4;
                    break;
                case 4:
                    $bonustier = 5;
                    break;
                case 5:
                    $bonustier = 0;
                    break;
            }

            $loginbonus = 0;
            $resettime = time();
            $success = '1';

            $updatearray = array(
                'UpdateBonus' => array(
                    'login' => $loginbonus,
                    'result' => $success
                    ),
                );

            $updatejson = json_encode($updatearray, 128);
            echo $updatejson;

            $stmt->execute();
            $stmt->close();
        }
    }

    function AddUser($hash)
    {
        global $link;
        global $hash;

        if($stmt = $link->prepare("INSERT INTO members (userid, loginbonus, bonustier, resettime) VALUES (?, ?, ?, ?)"))
        {
            $stmt->bind_param('ssss', $userid, $loginbonus, $bonustier, $resettime);

            $userid = $hash;
            $loginbonus = 1;
            $bonustier = 1;
            $resettime = time();

            $stmt->execute();
            $stmt->close();

            $this->CheckBonus($hash);
        }
    }

    function CheckFunc($func)
    {
        switch($func)
        {
            case cb:
                $this->CheckBonus($hash);
                break;
            case ub:
                $this->UpdateBonus($hash, $bonus);
                break;
            case au:
                $this->AddUser($hash);
                break;
            default:
                echo 'Function not found.';
                break;
        }
    }
}

$newObject = new CheckUser();
$newObject->CheckFunc($func);

?>

If anyone happens to see something that is wrong or could be done better, please let me know.如果有人碰巧看到错误的地方或可以做得更好的地方,请告诉我。 And yeah, I know I really should switch to using PDO.是的,我知道我真的应该改用 PDO。 I just started learning classes a functions so I feel I need to work on this first, then start learning PDO.我刚开始学习函数类,所以我觉得我需要先研究这个,然后开始学习 PDO。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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