简体   繁体   English

PHP,MYSQL工作台,邮递员无法正常工作

[英]PHP, MYSQL workbench, postman not working correctly

I have 2 users in a table, for example Vesna and Silvija, Vesna is first, Silvija second and if I type Silvija in postman he will return me "User does not exist and than Id of a user Silvija. This should be working on a way that Postman return just an Id of a User and if I type user that doesnt exist he should return that user doesnt exist. But when I type a users that doesnt actually exist he will return 2 times that users doesnt exist instead one time because table have 2 users that doesnt match entered user. Hope I have explained it understandably and that someone can help me. 我在一个表中有2位用户,例如Vesna和Silvija,Vesna是第一位,Silvija第二位,如果我在邮递员中键入Silvija,他将返回我“用户不存在,并且比用户Silvija的ID大。这应该在邮递员只返回一个用户ID的方式,如果我键入一个不存在的用户,他应该返回该用户不存在,但是当我键入一个不存在的用户时,他将返回该用户不存在的2次而不是一次,因为表有2位用户与输入的用户不匹配。希望我已经理解了,并且有人可以帮助我。

    foreach($oUsers as $oUser)
    {
        if($oUser->USERNAME == $sUsername)
        {

            $_SESSION['user_id'] = $oUser->USER_ID;
            $_SESSION['username'] = $oUser->USERNAME;
            echo $_SESSION['user_id'];
            break;



        }

        else
        {

            echo ' User does not exist';

        }

    }

You can not use echo '...' inside the foreach loop because echo is executed in every iteration. 您不能在foreach循环中使用echo '...' ,因为echo在每次迭代中都执行。

$found = false;
foreach($oUsers as $oUser){
    if($oUser->USERNAME == $sUsername){
        $_SESSION['user_id']  = $oUser->USER_ID;
        $_SESSION['username'] = $oUser->USERNAME;
        $found                = true;
        break;
    }
}

if($found === true){
    echo $_SESSION['user_id'];
}else{
    echo ' User does not exist';
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM