试图弄清楚为什么这会返回错误的计数......似乎限制对计数没有影响.
例如...如果找到20台计算机,但我将其限制为10(license = 10),我的返回显示20.
$stmt = $db->prepare("
SELECT count(computer_id)
FROM computers
WHERE account_id = :account_id
ORDER BY computer_id ASC LIMIT 0, :licenses
");
$binding = array(
'account_id' => $_SESSION['user']['account_id'],
'licenses' => $_SESSION['user']['licenses']
);
$stmt->execute($binding);
$results = $stmt->fetchAll(PDO::FETCH_COLUMN, 0);
Run Code Online (Sandbox Code Playgroud)
另一方面,这将正确返回10:
$stmt = $db->prepare("
SELECT computer_id
FROM computers
WHERE account_id = :account_id
ORDER BY computer_id ASC LIMIT 0, :licenses
");
$binding = array(
'account_id' => $_SESSION['user']['account_id'],
'licenses' => $_SESSION['user']['licenses']
);
$stmt->execute($binding);
$results = count($stmt->fetchAll(PDO::FETCH_COLUMN, 0));
Run Code Online (Sandbox Code Playgroud)
问题是您的查询结果只有一行,即计算机的总数.添加limit 10将不起作用,因为您没有超过10行,但只有一行.
但是,在第二个查询中,每台计算机只有一行.在这种情况下,limit 10将您的行数限制为10.
你可以做的是这样的事情:
SELECT LEAST(10, count(computer_id))
FROM computers
WHERE account_id = :account_id
Run Code Online (Sandbox Code Playgroud)
如果使用此许可证的计算机少于10台,则结果将小于10;如果至少有10台计算机,则结果将为10.