Jump to content

bind_result inside of a function


Solar
Go to solution Solved by ginerjm,

Recommended Posts

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 🙂

Link to comment
Share on other sites

  • Solution

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 by ginerjm
  • Like 1
Link to comment
Share on other sites

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 by Solar
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.