Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 我应该使用elseif并在某个地方关闭数据库连接吗?_Php_Mysql - Fatal编程技术网

Php 我应该使用elseif并在某个地方关闭数据库连接吗?

Php 我应该使用elseif并在某个地方关闭数据库连接吗?,php,mysql,Php,Mysql,我想知道是否有更好的方法来编写下面的代码。基本上有两个问题,我应该使用elseif吗?我是否需要在某个地方关闭与数据库的连接 多谢各位 更新代码 if ($result && mysql_num_rows($result) === 1) { $member = mysql_fetch_assoc($result); if ($member['disabled']) { header("location: not-allowed.php");

我想知道是否有更好的方法来编写下面的代码。基本上有两个问题,我应该使用
elseif
吗?我是否需要在某个地方关闭与数据库的连接

多谢各位

更新代码

if ($result && mysql_num_rows($result) === 1) {


    $member = mysql_fetch_assoc($result);

    if ($member['disabled']) {
        header("location: not-allowed.php");
        exit();
        }

    if (!$member['verified']) {
        header("location: please-verify.php");
        exit();
        }
}
不要重复你自己

if (!($result && mysql_num_rows($result) == 1)) redirect("login-failed.php");
$member = mysql_fetch_assoc($result);
if ($member['disabled']) redirect("not-allowed.php");
if (!$member['verified']) redirect("please-verify.php");
if ( ($member['expires']) && ($member['expires'] <= time()) ) redirect("expired.php");
if ( ($_SESSION['SESS_TOKEN'] == $_POST['token']) ) redirect("member-index.php");

function redirect($location){
    header("location: $location");
    exit();
}
if(!($result&&mysql_num_rows($result)==1))重定向(“login failed.php”);
$member=mysql\u fetch\u assoc($result);
if($member['disabled'])重定向(“not allowed.php”);
如果(!$member['verified'])重定向(“请验证.php”);

如果($member['expires'])&($member['expires']可以有多个结果吗?您是否通过唯一键进行查询?@Kerrek SB Yes将只有一个结果。那么为什么要检查任何结果计数?您只需在(获取…)时执行通常的
loop,在循环中,当您确定登录成功时,您设置了一个标志。在循环之后,您只需测试标志。@Kerrek SB我已经更新了我的代码。您不认为现在更改代码就足够简单了吗?您甚至可以说
if($result&&$member=mysql\u fetch\u assoc($result))
now…如果我替换die()使用
标题(“location:login failed.php”);exit();
?您必须再次将它们放在括号中,但是是的-它会起作用。我想您的评论是针对我以前的评论的。好的,是的-这就是您询问它的地方:)好的,在接受您的回答之前,还有最后一件事。我已经更新了问题中的代码。我应该在某个地方关闭数据库连接还是不重要(意味着它自己关闭)。我遇到了错误分析错误:语法错误,意外的“!”,预期的“(
!”($result&&mysql\u num\u rows($result)==1)
哦,是的,需要额外的一对括号。很难看,但是works@Nikolai在这样的代码中,无论编写什么,都没有速度方面的问题。它不会以任何方式影响您的性能。我必须考虑其他事情。谢谢!
if (!($result && mysql_num_rows($result) == 1)) redirect("login-failed.php");
$member = mysql_fetch_assoc($result);
if ($member['disabled']) redirect("not-allowed.php");
if (!$member['verified']) redirect("please-verify.php");
if ( ($member['expires']) && ($member['expires'] <= time()) ) redirect("expired.php");
if ( ($_SESSION['SESS_TOKEN'] == $_POST['token']) ) redirect("member-index.php");

function redirect($location){
    header("location: $location");
    exit();
}