fife Posted February 11, 2011 Share Posted February 11, 2011 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']; ?> Quote Link to comment https://forums.phpfreaks.com/topic/227346-new-to-functions/ Share on other sites More sharing options...
Nodral Posted February 11, 2011 Share Posted February 11, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/227346-new-to-functions/#findComment-1172670 Share on other sites More sharing options...
KevinM1 Posted February 11, 2011 Share Posted February 11, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/227346-new-to-functions/#findComment-1172677 Share on other sites More sharing options...
PFMaBiSmAd Posted February 11, 2011 Share Posted February 11, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/227346-new-to-functions/#findComment-1172691 Share on other sites More sharing options...
fife Posted February 11, 2011 Author Share Posted February 11, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/227346-new-to-functions/#findComment-1172697 Share on other sites More sharing options...
Nodral Posted February 11, 2011 Share Posted February 11, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/227346-new-to-functions/#findComment-1172700 Share on other sites More sharing options...
PFMaBiSmAd Posted February 11, 2011 Share Posted February 11, 2011 $User = GetUser($_SESSION['MM_Username']); ^^^ You are assigning the results that GetUser() returns to the variable named $User, not $UserInfo, You would need to use - echo $User['forename']; echo $User['surname']; Quote Link to comment https://forums.phpfreaks.com/topic/227346-new-to-functions/#findComment-1172702 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.