For some reason, my variable $userinfo isn't going global, and won't echo on test.php Here's my code: test.php include('includes/functions.php'); getUser($userid); echo $userinfo['name']; Code (markup): functions.php function getUser($uid) { global $userinfo; $checkuser="SELECT * FROM fct_users WHERE ownerid='$uid'"; $checkuser2=mysql_query($checkuser) or die("Could not check user"); $checkuser3=mysql_num_rows($checkuser2); if($checkuser3 > 0) { $query2 = "SELECT * FROM fct_trains WHERE ownerid='$uid'"; $result2 = mysql_query($query2) or die(mysql_error()); while($row = mysql_fetch_array($result2)) { $userinfo['name'] = $row['name']; } } } Code (markup): Suggestions? Thanks!
try $userinfo = array(); include('includes/functions.php'); getUser($userid); echo $userinfo['name']; Code (markup):
nope it doesn't, if I put this: echo $row['name']; Code (markup): inside my mysql_fetch_array loop, it executes correctly.
<? function getUser($uid) { $checkuser_sql = "SELECT * FROM `fct_users`,`fct_trains` WHERE `fct_trains`.`ownerid` = '$uid' and `fct_users`.`ownerid` = '$uid' limit 1"; $checkuser_result = mysql_query($checkuser_sql); if(mysql_num_rows($checkuser_result) > 0) { $user = mysql_fetch_assoc($checkuser_result); return $user['name']; } return array('name'=>'User not found'); } $userinfo = Array(); $userinfo = getUser(intval($uid)); ?> Code (markup): Using globals like you are doing isn't a good idea. You have to pass the function like the above even if blank. Then return the data that you want. Even then you can combine the sql statement into one statement rather then two statements.