PHP, MYSQL workbench, postman not working correctly

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.


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







1 Answer
1



You can not use echo '...' inside the foreach loop because echo is executed in every iteration.


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





Thank you, it works like a charm :D
– Vanessa
Sep 2 at 18:18





You're welcome.
– 김라댜
Sep 2 at 18:28



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Some of your past answers have not been well-received, and you're in danger of being blocked from answering.



Please pay close attention to the following guidance:



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)