嘿所以我試圖檢索用戶已有多少個密鑰以及他們的帳戶允許他們擁有多少個密鑰,然後計算百分比並將其顯示出來,而我知道這個代碼下面PHP/MySQL - 不斷返回0
$SQL1 = $odb -> prepare("SELECT `licenses` FROM `users` WHERE `username` = :username");
$SQL1 -> execute(array(':username' => $username));
$maxKeys = $SQL1 -> fetchColumn(0);
出於某種原因,下面的代碼繼續返回0甚至認爲有表中的多個條目我目前的用戶作爲所有者
$SQL = $odb -> prepare("SELECT COUNT(*) FROM `licensetable` WHERE `owner` = $username");
$SQL -> execute(array());
$keysUsed = $SQL -> fetch(PDO::FETCH_ASSOC);
我知道它是這麼簡單的東西,但我無法想象出我的生活。 的表結構如下
SELECT `id`, `key`, `owner` FROM `licensetable`
完整的原始頁面的源代碼
<?php
ob_start();
require_once('include/db.php');
require_once('include/initialize.php');
if(!($user->LoggedIn())){
header('location: login');
die();
}else{
$username = $_SESSION['username'];
$SQL = $odb -> prepare("SELECT COUNT(*) FROM `licensetable` WHERE `owner` = $username");
$SQL -> execute(array());
$keysUsed = $SQL -> fetch(PDO::FETCH_ASSOC);
$SQL1 = $odb -> prepare("SELECT `licenses` FROM `users` WHERE `username` = :username");
$SQL1 -> execute(array(':username' => $username));
$maxKeys = $SQL1 -> fetchColumn(0);
$keyPercentage = ($keysUsed/$maxKeys) * 100;
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>SQL</title>
<link href="css/bootstrap.min.css" rel="stylesheet">
<link href="css/styles.css" rel="stylesheet">
</head>
<body>
<div class="row">
<div class="col-lg-12">
<h1 class="page-header">Home</h1>
</div>
</div>
<div class="row">
<div class="col-md-9">
<?php echo $keyPercentage;
echo $maxKeys;
?>
</div>
</div>
</div>
</body>
</html>
所以這個字段是'username'或'owner'? –
@u_mulder在許可表(代碼的第二部分)中,該字段是所有者 – ilernincplusminus
爲什麼訪問對象方法時有空格? '$ maxKeys = $ SQL1 - > fetchColumn(0)'。這不會導致你的問題,但這是不好的做法。你不需要空間。 – BugHunterUK