我见过有人这样使用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()无论如何都是缓存的,因此不需要将其存储在本地变量中。
我认为将rowCount()
存储在局部变量中总是更快,因为您不会有函数调用的开销
逻辑上你在这个例子中做的方式应该更快,因为$result
只是一个需要读取的内存位置,即超级快,其中rowCount()
如果需要处理和返回数据的函数