Jump to content

Recommended Posts

Hi.  I am trying to write a function but they are new to me and confusing me.  On multiple pages I am requesting users details.  I thought instead of writing out the query every time I want to do this I would include a functions page with the query in it.  The only variable I carry from the user is the user name as a session when they log in.  The user name is the email address of the person signed in.

 

so here is my first function.  As im sure you can see it doesn't work but I don't understand why or how to call it properly.  Can someone please explain....

 

function GetUser($user = "")
 {	$qFindUser = "SELECT * FROM members WHERE email = '$user'";
 	$rFindUser = mysql_query($qFindUser) or die (mysql_error());
  	$UserInfo = mysql_fetch_array($rFindUser);
  }

 

calling the function to return all details of a member as an array.

<?php  
$Name = $_SESSION['MM_Username'];
$UserInfo = GetUser($Name);


echo $UserInfo['forename'];
?>

 

Link to comment
https://forums.phpfreaks.com/topic/227346-new-to-functions/
Share on other sites

Hi

 

I'm fairly new to all this myself, but I'd say there are 2 problems.

 

1.  You are not actually instructing the function to return any values.

2.  You cannot return an array from a function (or so I've been told).

 

Hope this points you in the right direction.

Link to comment
https://forums.phpfreaks.com/topic/227346-new-to-functions/#findComment-1172670
Share on other sites

Hi

 

I'm fairly new to all this myself, but I'd say there are 2 problems.

 

1.  You are not actually instructing the function to return any values.

2.  You cannot return an array from a function (or so I've been told).

 

Hope this points you in the right direction.

 

Yes, you can most certainly return an array from a function.  In fact, doing so is the solution to the OP's problem.  Smack whoever said you couldn't on the back of the head.

 

To the OP: simply write:

 

return $UserInfo;

 

At the bottom of your function.

Link to comment
https://forums.phpfreaks.com/topic/227346-new-to-functions/#findComment-1172677
Share on other sites

or so I've been told

 

The interesting thing about programming is you can try anything you want in just a handful of seconds and observe for yourself if it does what you expect or not. You don't have to guess, assume, or take someone else's (mistaken) word for what works or does not work.

Link to comment
https://forums.phpfreaks.com/topic/227346-new-to-functions/#findComment-1172691
Share on other sites

Brillient thank you but I'm still having trouble with calling the array individually.  heres what I have now

 

function GetUser($user = "")
 {	$qFindUser = "SELECT * FROM members WHERE email = '$user'";
 	$rFindUser = mysql_query($qFindUser) or die (mysql_error());
  	$UserInfo = mysql_fetch_array($rFindUser);
	return $UserInfo;
  }

 

So to feed the function my parameters..

$User = GetUser($_SESSION['MM_Username']);


					echo $UserInfo['forename'];
					echo $UserInfo['surname'];

 

I'm sorry but functions are really confusing me at the minute. lol.  I will not stop till I understand them though.

 

Link to comment
https://forums.phpfreaks.com/topic/227346-new-to-functions/#findComment-1172697
Share on other sites

Try

 

function GetUser($user)	 {	
$qFindUser = "SELECT * FROM members WHERE email = '$user'";	 	
$rFindUser = mysql_query($qFindUser) or die (mysql_error());	  	
$UserInfo = mysql_fetch_array($rFindUser);		
return $UserInfo;	  }

 

This way you are just defining the fuction as requiring a variable called $user which it will pass into itself.  I don't know what the

=""

is for but that may be causing you an issue.

Link to comment
https://forums.phpfreaks.com/topic/227346-new-to-functions/#findComment-1172700
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.