graham23s Posted May 21, 2007 Share Posted May 21, 2007 Hey Guys, there is a piece of code i use a lot in the sites i make lately it's this: // Get the users details from mysql...////////////////////////////////////////////// $query1 = "SELECT * FROM `membership` WHERE `username`='$member'"; $result1 = mysql_query($query1) or die (mysql_error()); $row = mysql_fetch_array($result1); $id = $row['id']; $username = $row['username']; just to find out the username and id of the logged in user, is there anyway i can make this a function, functions is one thing i haven't really mastered yet any help would be great cheers Graham Link to comment https://forums.phpfreaks.com/topic/52377-solved-making-a-function/ Share on other sites More sharing options...
pocobueno1388 Posted May 21, 2007 Share Posted May 21, 2007 <?php function userName($member){ $query1 = "SELECT * FROM `membership` WHERE `username`='$member'"; $result1 = mysql_query($query1) or die (mysql_error()); $row = mysql_fetch_array($result1); $id = $row['id']; $username = $row['username']; echo "$username ($id)"; } ?> Now just call it like this: userName(persons_username); Link to comment https://forums.phpfreaks.com/topic/52377-solved-making-a-function/#findComment-258446 Share on other sites More sharing options...
JakeTheSnake3.0 Posted May 21, 2007 Share Posted May 21, 2007 function getUserInfo($memberID) { $infoArray = array(); // Get the users details from mysql...////////////////////////////////////////////// $query = "SELECT * FROM membership WHERE username='$memberID'"; $result = mysql_query($query) or die (mysql_error()); $row = mysql_fetch_array($result); $infoArray['id'] = $row['id']; $infoArray['username'] = $row['username']; return $infoArray; } What this function does, is that it accepts a single argument; it then takes that argument and does an SQL query using it. In the end, it 'returns' an array holding the appropriate information. When a function 'returns' something, it's basically the equivalent of storing variable data..... ex) $userInfo = getUserInfo('100454998'); This will make $userInfo an array....so now if you want to access the username, do this....echo($userInfo['username']); Link to comment https://forums.phpfreaks.com/topic/52377-solved-making-a-function/#findComment-258449 Share on other sites More sharing options...
Wildbug Posted May 21, 2007 Share Posted May 21, 2007 I'm not sure that $id and $username will be updated outside of that function. (Variable scope.) You may need to declare them "global $id,$username" at the beginning of the function. Otherwise, you could also return the two values and capture them with a "list($id,username) = yourFunction($member)". Link to comment https://forums.phpfreaks.com/topic/52377-solved-making-a-function/#findComment-258450 Share on other sites More sharing options...
graham23s Posted May 21, 2007 Author Share Posted May 21, 2007 Thanks for the quick replies guys, that has given me a lot to work on. thanks again Graham Link to comment https://forums.phpfreaks.com/topic/52377-solved-making-a-function/#findComment-258454 Share on other sites More sharing options...
per1os Posted May 21, 2007 Share Posted May 21, 2007 Or you could pass them by reference, just another option. <?php function getUserInfo($memberID, &$username, &$id) { // Get the users details from mysql...////////////////////////////////////////////// $query = "SELECT * FROM membership WHERE username='$memberID'"; $result = mysql_query($query) or die (mysql_error()); $row = mysql_fetch_array($result); $id = $row['id']; $username = $row['username']; return true; } getUserInfo($memberID, $username, $id); echo 'The username is: ' . $username . ' The ID is ' . $id; ?> Fun stuff this programming is. Link to comment https://forums.phpfreaks.com/topic/52377-solved-making-a-function/#findComment-258455 Share on other sites More sharing options...
graham23s Posted May 21, 2007 Author Share Posted May 21, 2007 lol that's very true, everytime i do something on my own (and it works) i get paranoid i have done it wrong (even though it works) incase there was a better way of doing things or later on down the line it would have been better doing it another way lol cheers Graham Link to comment https://forums.phpfreaks.com/topic/52377-solved-making-a-function/#findComment-258458 Share on other sites More sharing options...
Wildbug Posted May 21, 2007 Share Posted May 21, 2007 I don't think it was mentioned above, but include()s are also an option for oft-repeated code. Link to comment https://forums.phpfreaks.com/topic/52377-solved-making-a-function/#findComment-258481 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.