开发者

rowCount() as a variable or including it directly in if statements which is better?

开发者 https://www.devze.com 2023-04-03 02:39 出处:网络
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 t

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>';
    }

Whi开发者_如何转开发ch 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.


I think it will always be quicker to store the rowCount() in a local variable as you won't have the overhead of the function call


Logically doing it the way you have in this example should be faster because $result is just a location in memory that needs to be read, ie uber fast, where as rowCount() if a function that needs to process and return data.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号