[英]Class function not printing out data in php with mysqli prepared statement
我需要有關不回顯$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);
?>
如果你碰巧對我的問題投反對票,你能告訴我為什么嗎? 我在這里做錯了什么,我可以做得更好,在提問時我可以做更多的工作嗎?
我相信我已經弄清楚我做錯了什么。 它現在似乎按預期工作。 我移動了$stmt->store_result(); 在if($stmt->num_rows != 0)行上方,我將該行更改為if($stmt->num_rows > 0) 。 不確定第二個更改是否真的有任何效果,但在語句中使用 > over != 似乎更好。
這是處於工作狀態的代碼。
//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);
?>
如果有人碰巧看到錯誤的地方或可以做得更好的地方,請告訴我。 是的,我知道我真的應該改用 PDO。 我剛開始學習函數類,所以我覺得我需要先研究這個,然后開始學習 PDO。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.