我见过人们这样使用 rowCount...
if ($q -> rowCount() < 1) {
....
}
这就是我在大多数情况下使用它的方式,我的问题是,如果您有多个 elseif 语句,是否最好将其存储在变量中,因为 rowCount 不会被多次调用?像这样...
if (isset($_GET['action']) && $_GET['action'] === 'addhotlink') {
$q = $dbc -> prepare("SELECT * FROM hotlinks WHERE id = ?");
$q -> execute(array($details['id']));
$result = $q -> rowCount();
if ($result < 3) {
echo '<p>Choose a name for your hotlink (max 15 characters):</p><p><form action="/success?action=hotlink" method="post"><input type="text" name="link_name" maxlength="15" /></p><input type="hidden" name="addhotlink" value="' . $_SERVER['HTTP_REFERER'] . '" /><p><input id="submit" type="submit" value="Add Hotlink" /></p></form>';
}
elseif ($result < 10 && $details['subscriber'] > 0) {
echo '<p>Choose a name for your hotlink (max 15 characters):</p><p><form action="/success?action=hotlink" method="post"><input type="text" name="link_name" maxlength="15" /></p><input type="hidden" name="addhotlink" value="' . $_SERVER['HTTP_REFERER'] . '" /><p><input id="submit" type="submit" value="Add Hotlink" /></p></form>';
}
elseif ($result > 9 && $details['subscriber'] > 0) {
echo '<p>You have already used your maximum number of 10 hotlinks.</p>';
}
else {
echo '<p>You have already used your maximum amount of hotlinks allowed for non subscribed members.</p><p>To get more please <a href"#">subscribe</a>.</p>';
}
哪个更好,为什么?
最佳答案
如果您只需要行数而不是真实数据,您应该更喜欢简单的“SELECT COUNT(*)”。 AFAIK rowCount() 无论如何都会被缓存,因此不需要将其存储在局部变量中。
关于php - rowCount() 作为变量或直接将其包含在 if 语句中哪个更好?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7339012/