amilaik Posted January 30, 2011 Share Posted January 30, 2011 Hi all, I'm trying to write a function to return an array. But it keeps returning an empty array with NULL values .... This is my code. function ShowUserData ($uid) { $con = mysql_connect("localhost","user","pass"); if (!$con) { die('Oops !! something went wrong with the DB connection, Server said ' . mysql_error()); } mysql_select_db("db", $con); $password = md5($password); $query = mysql_query("SELECT * FROM `table` WHERE `uid` = '".$uid."'"); $num = mysql_num_rows($query); while ($row = mysql_fetch_array($query, MYSQL_NUM)) { $outArray = array('uid' =>$row['uid'], 'email' => $row['email'], 'name' => $row['name']); } return $outArray; } what am i doing wrong? Quote Link to comment https://forums.phpfreaks.com/topic/226104-adding-array-members-in-a-while-loop/ Share on other sites More sharing options...
Pikachu2000 Posted January 30, 2011 Share Posted January 30, 2011 Show an example of how you're calling the function, and how you're displaying the values it returns. Quote Link to comment https://forums.phpfreaks.com/topic/226104-adding-array-members-in-a-while-loop/#findComment-1167199 Share on other sites More sharing options...
amilaik Posted January 30, 2011 Author Share Posted January 30, 2011 $adminFunctions = new AdminFunctions(); $userdata = $adminFunctions->ShowUserData($_REQUEST['token']); var_dump($userdata); Quote Link to comment https://forums.phpfreaks.com/topic/226104-adding-array-members-in-a-while-loop/#findComment-1167202 Share on other sites More sharing options...
jcbones Posted January 30, 2011 Share Posted January 30, 2011 You are asking for a numeric array, and trying to assign associative indexes. Change your while loop to: while ($row = mysql_fetch_assoc($query)) { $outArray[] = $row; //IF you only expect 1 row, you don't need a while loop, and you don't need the brackets on $outArray. } Quote Link to comment https://forums.phpfreaks.com/topic/226104-adding-array-members-in-a-while-loop/#findComment-1167204 Share on other sites More sharing options...
amilaik Posted January 30, 2011 Author Share Posted January 30, 2011 Hello JcB, still returns NULL ... Quote Link to comment https://forums.phpfreaks.com/topic/226104-adding-array-members-in-a-while-loop/#findComment-1167208 Share on other sites More sharing options...
nankoweap Posted January 30, 2011 Share Posted January 30, 2011 looks like a scoping issue, but even if you get that to work, you're still only going to get the last returned row in the array. try something like this: $outArray = array(); while ($row = mysql_fetch_array($query, MYSQL_NUM)) { $outArray[] = array('uid' =>$row['uid'], 'email' => $row['email'], 'name' => $row['name']); } return $outArray; so you're essentially returning an array of arrays. you can loop through them like this: foreach ($outArray as $row) { // do something with the row elements (e.g. $row['uid']) } something like the above cures both issues. Quote Link to comment https://forums.phpfreaks.com/topic/226104-adding-array-members-in-a-while-loop/#findComment-1167211 Share on other sites More sharing options...
Pikachu2000 Posted January 30, 2011 Share Posted January 30, 2011 Have you echoed $_REQUEST['token'] to be certain it has a value when it's passed as an argument? Quote Link to comment https://forums.phpfreaks.com/topic/226104-adding-array-members-in-a-while-loop/#findComment-1167216 Share on other sites More sharing options...
jcbones Posted January 30, 2011 Share Posted January 30, 2011 looks like a scoping issue, but even if you get that to work, you're still only going to get the last returned row in the array. try something like this: $outArray = array(); while ($row = mysql_fetch_array($query, MYSQL_NUM)) { //<--STILL PULL NUMERICAL ARRAY $outArray[] = array('uid' =>$row['uid'], 'email' => $row['email'], 'name' => $row['name']); //<--STILL TRYING TO ASSIGN ASSOC INDEXES } return $outArray; Before you go any further, lets do some de-bugging. Top of script: echo '<pre>'; print_r($_REQUEST); echo '</pre>'; Change query call to: $query = mysql_query("SELECT * FROM `table` WHERE `uid` = '".$uid."'") or die(mysql_error()); And of course as Indicated above. Change your fetch call: //Either: while ($row = mysql_fetch_array($query, MYSQL_ASSOC)) { //or while ($row = mysql_fetch_assoc($query)) { Quote Link to comment https://forums.phpfreaks.com/topic/226104-adding-array-members-in-a-while-loop/#findComment-1167217 Share on other sites More sharing options...
amilaik Posted January 30, 2011 Author Share Posted January 30, 2011 hay nankoweap's code worked .... i just had t change the MYSQL_NUM to a MYSQL_ASSOC thanks guys .... you guys rock !!! Quote Link to comment https://forums.phpfreaks.com/topic/226104-adding-array-members-in-a-while-loop/#findComment-1167219 Share on other sites More sharing options...
nankoweap Posted January 30, 2011 Share Posted January 30, 2011 cool. ya. i don't use the mysql_* functions. i use the mysqli extension exclusively so i couldn't help you there. glad it's working. Quote Link to comment https://forums.phpfreaks.com/topic/226104-adding-array-members-in-a-while-loop/#findComment-1167221 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.