Php rowCount()作为变量或直接包含在if语句中哪个更好?
我见过有人这样使用rowCountPhp rowCount()作为变量或直接包含在if语句中哪个更好?,php,mysql,performance,pdo,Php,Mysql,Performance,Pdo,我见过有人这样使用rowCount if ($q -> rowCount() < 1) { .... } if (isset($_GET['action']) && $_GET['action'] === 'addhotlink') { $q = $dbc -> prepare("SELECT * FROM hotlinks WHERE id = ?"); $q -> execute(array($details['id']));
if ($q -> rowCount() < 1) {
....
}
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>';
}
if($q->rowCount()<1){
....
}
我的问题是,如果你有多个elseif语句,最好将它存储在一个变量中,因为rowCount不会被调用多次??像这样
if ($q -> rowCount() < 1) {
....
}
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>';
}
if(isset($\u GET['action'])&&&$\u GET['action']=='addhotlink')){
$q=$dbc->prepare(“从id=?”的热链接中选择*”;
$q->execute(数组($details['id']);
$result=$q->rowCount();
如果($result<3){
echo'为您的热链接选择一个名称(最多15个字符):;
}
elseif($result<10&&$details['subscriber']>0){
echo'为您的热链接选择一个名称(最多15个字符):;
}
elseif($result>9&&$details['subscriber']>0){
echo'您已经使用了最多10个热链接。;
}
否则{
echo'您已经使用了非订阅会员所允许的最大热链接量。请获取更多信息。;
}
哪一个更好,为什么??我认为存储
行数()总是更快
在局部变量中,因为您不会有函数调用的开销在本例中逻辑上这样做应该更快,因为$result
只是内存中需要读取的一个位置,即uber fast,其中asrowCount()
如果函数需要处理和返回数据。如果您只需要行数而不需要实际数据,则应选择简单的“选择计数(*)”。
AFAIK行计数()无论如何都是缓存的,因此不需要将其存储在局部变量中。基准测试和分析将提供精确的答案。箭头操作符之间的间隔对于观看
a->a来说非常糟糕。
这是我喜欢的方式,每个人都有读写代码的方式,我就是这样做的,没有正确或错误的方式嗯。