使用mysqli预处理语句,类函数不会在php中打印出数据

时间:2016-05-30 11:36:34

标签: php function oop mysqli

我需要一些不回显 $ bonusjson 数组的函数的帮助。该函数是 CheckBonus()。除了这个之外,所有其他功能都可以正常工作。当它运行时它没有任何回声,我无法弄清楚为什么。这是我第一次使用类和函数。 CheckBonus()假设查询数据库并检查表以查看 $ hash 变量数据是否存在,然后运行查询并打印出数据。

我认为问题出在 if($ stmt-> num_rows!= 0)。我也尝试使用 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);

?>

如果你碰巧忘记我的静止,你能让我知道为什么吗?我在这里做错了什么,我能做得更好,在提问时我能做些什么呢?

1 个答案:

答案 0 :(得分:0)

我相信我弄清楚我做错了什么。它现在似乎按预期工作。我将 $ stmt-&gt; store_result(); 移到 if($ stmt-&gt; num_rows!= 0)行上方,然后我将该行更改为 if ($ stmt-&gt; num_rows&gt; 0)。不确定第二次更改是否确实有任何影响,但使用&gt;似乎更好结束!=在声明中。

这是处于工作状态的代码。     

FrameLengthInBytes = 144 * BitRate / SampleRate + Padding

=> For 128 kbit/s, 48Khz, 0 padding, it gives 384 bytes

如果有人碰巧看到了错误或可以做得更好的事情,请告诉我。是的,我知道我真的应该转而使用PDO。我刚刚开始学习一个函数类,所以我觉得我需要先解决这个问题,然后开始学习PDO。