Solar Posted October 13, 2021 Share Posted October 13, 2021 Let's say I have a simple statement. $stmt = $con->prepare('SELECT email,rank FROM accounts WHERE id = ?'); $stmt->bind_param('i', $_SESSION['id']); $stmt->execute(); $stmt->bind_result($email,$rank); $stmt->fetch(); $stmt->close(); I can then echo the e-mail and rank variables very easily in PHP <?=$email?> <?=$rank?> What is the best way to echo the results with a bind_result while it's inside of a function? function getUserInfo(){ global $con; $stmt = $con->prepare('SELECT email,rank FROM accounts WHERE id = ?'); $stmt->bind_param('i', $_SESSION['id']); $stmt->execute(); $stmt->bind_result($email,$rank); $stmt->fetch(); $stmt->close(); } I could easily stick email and rank into an array and return it but that's duplicating code. Is there a way to fetch the bind_result and return all variables inside of it? I greatly appreciate it 🙂 Quote Link to comment https://forums.phpfreaks.com/topic/313980-bind_result-inside-of-a-function/ Share on other sites More sharing options...
gw1500se Posted October 13, 2021 Share Posted October 13, 2021 $result=$stmt->fetchAll(); echo "<pre>"; print_r($result); echo "</pre>"; Quote Link to comment https://forums.phpfreaks.com/topic/313980-bind_result-inside-of-a-function/#findComment-1591003 Share on other sites More sharing options...
Solution ginerjm Posted October 13, 2021 Solution Share Posted October 13, 2021 (edited) Return the vars as arguments of the function with the & on each. "function xyz(&$a, &$b)" Call the function with "function ($a, $b)" and then use $a & $b in your following code. And - how does using an array of your two values cause duplication of code? Anyway - your question is either how do I return the values or how do I echo the values? Which do you want to do? Edited October 13, 2021 by ginerjm 1 Quote Link to comment https://forums.phpfreaks.com/topic/313980-bind_result-inside-of-a-function/#findComment-1591004 Share on other sites More sharing options...
Solar Posted October 13, 2021 Author Share Posted October 13, 2021 (edited) 6 minutes ago, ginerjm said: Return the vars as arguments of the function with the & on each. "function xyz(&$a, &$b)" Call the function with "function ($a, $b)" and then use $a & $b in your following code. As if it's that simple! Thanks a ton! Edit: I guess either way, I still have to specify the variables. I'm good with this! $account = new Account(); $account->getUserInfo($email,$rank); class Account{ function getUserInfo(&$email,&$rank){ global $con; $stmt = $con->prepare('SELECT email, rank FROM accounts WHERE id = ?'); $stmt->bind_param('i', $_SESSION['id']); $stmt->execute(); $stmt->bind_result($email, $rank); $stmt->fetch(); $stmt->close(); } } Edited October 13, 2021 by Solar Quote Link to comment https://forums.phpfreaks.com/topic/313980-bind_result-inside-of-a-function/#findComment-1591005 Share on other sites More sharing options...
ginerjm Posted October 13, 2021 Share Posted October 13, 2021 (edited) Yes it was that simple. To keep your function cleaner I'd add the id value as an argument of the function as well function ($id, &$mail, &$rank) Edited October 13, 2021 by ginerjm 1 Quote Link to comment https://forums.phpfreaks.com/topic/313980-bind_result-inside-of-a-function/#findComment-1591006 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.