Global Variable not been displayed in another file not showing global variable

Hi,

I have an issue with a development function i am working on i have a function which basically contains users information which contaiins user information but when i have it in a html form it doesnt print out the information as i hoped.

This is my php function


function userinfo()
{
	global $user,$rank,$username,$password,$email,$aim,$msn,$mid,$recruitedby,$joineddate,$disable,$logindays,$web;
	include("dbconnect.php");
		$username=$_SESSION['user'];
		$u="SELECT * FROM members WHERE username='$user'";
		$rs=mysqli_query($con,$u);
		if(!$rs)
		{
			//
			echo "Error".mysqli_error($con);
			//	
		}
		else
		{
			//
			$count=$rs->num_rows;
			if($count>0)
			{
				while($data=$rs->fetch_assoc())
				{
					//
					$user = $data['username'];
					$rank = $data['rank'];
					$username = $data['username'];
					$password = $data['password'];
					$email=$data['email'];
					$aim=$data['aim'];
					$msn=$data['msn'];
					$mid=$data['mid'];
					$recruitedby=$data['Recruitedby'];
					$joineddate=$data['joineddate'];
					$disable=$data['disable'];
					$logindays=$data['logindays'];
					$web=$data['web'];
					//
				}
				
			}
			//	
		}
}

When i try to call it from another file called membersarea they html code i am using is


<?
userinfo();
?>
<table width="390" border="1">
			<tr>
				<th colspan="2">Your Profile</th>
			</tr>
			<tr>
				<td>Member</td>
				<td><? echo $username;?></td>
			</tr>
			<tr>
				<td>Email</td>
				<td><? echo $email;?></td>
			</tr>
			<tr>
				<td>Recruited By:</td>
				<td><? echo $recruitedby;?></td>
			</tr>
			<tr>
				<td>Date Joined</td>
				<td><? echo $joineddate;?></td>
			</tr>

When i preview it on my site i cant see what $email and $recruitedby Values are being displayed for the members information?

i thought this would be a good way instead of re-using the same code all the time?

Can anyone tell me what am i doing wrong?

I don’t see where you are including the file which contains your userinfo() function.


<?php  // <- best to use the full opening php tag

include 'userinfo_function.php'; 
userinfo();

?>

As for am I doing it right?, well using globals is usually not a good idea because something somewhere else can suddenly appear and overwrite those globals.

Your userinfo() function could variously, ie some ideas

a) connect to mysqli and return object members instead of arrays
b) return the object with those members (ie return $data->username or $data[‘username’])
c) you could extract the vars out of the returned array with extract($data)
d) if you are not doing it elsewhere, you could sanitize/validate the $_SESSION[‘user’] before going further and return false or some messages in case of failure to find the user


$d = userinfo();

if( !$d ){  // if userinfo returns false if it failed for any reason
// fail, redirect or something

exit();
}

otherwise use the array you already have, $d[‘username’] or as I said, extract( $d );

Simple example:


$d= array('a'=>1,'b'=>2);

extract($d);

echo $a . ' and ' . $b; 
// gives 
1 and 2

Its a matter of taste whether you want to return an object and alter your echo statements to $d->username or have the extra extract line and have all of your vars simpler $username.