I have seen people use rowCount like this…
if ($q -> rowCount() < 1) {
....
}
Which is how I use it for most part my question is though if you have multiple elseif statements is it better to store it in a variable as the rowCount will not be called more than once?? Like this…
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>';
}
Which is better and why??
If you just need the number of rows and not the real data, you should prefer a simple “SELECT COUNT(*)”.
AFAIK rowCount() is cached anyway, so there is no need to store it in a local variable.