Jump to content

Recommended Posts

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

<?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);

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']);

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)".

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.

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

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.