runsum Posted February 3, 2010 Share Posted February 3, 2010 Hi All, I have a function that queries a mysql database through an ADOdb connection. I am trying to pass the resulting array out of the function. However it seems that the array is lost on return. Here are the relevant snippits of code: function search_username($username) { global $DB; // This is the ADOdb connection that is defined elsewhere $query = "SELECT id, site_id, test_site_id, control, partner_id FROM user where username='$username'"; $result = $DB->Execute($query) or die("Error in query: $query. " . $DB->ErrorMsg()); print_r($result->fields); return $result; Back to the main code. $username = $_POST['username']; $resulta = search_username($username); //calls the function echo $resulta; $user_id = $result->fields[0]; echo '<br />user id: ' . $user_id; So the print_r command in the function, before the return gives: Array ( [0] => 1 [id] => 1 [1] => 1 [site_id] => 1 [2] => 0 [test_site_id] => 0 [3] => [control] => [4] => 2 [partner_id] => 2 ) but the echo command after the return gives id,site_id,test_site_id,control,partner_id 1,1,0,,2 BTW, a print_r of $resulta after the return gives the parameters of the ADOrecordset_mysql The userid variable never gets a value. Any ideas? Link to comment https://forums.phpfreaks.com/topic/190732-losing-array-on-function-return/ Share on other sites More sharing options...
akitchin Posted February 3, 2010 Share Posted February 3, 2010 this will seem silly, but you're using $result rather than $resulta to assign the value to $user_id... typo? Link to comment https://forums.phpfreaks.com/topic/190732-losing-array-on-function-return/#findComment-1005836 Share on other sites More sharing options...
runsum Posted February 3, 2010 Author Share Posted February 3, 2010 Grin... only a bit of a typo. I originally used $result both in the function and in the main code. I though maybe I was creating the conflict by using the variable name in both places so I changed the variable to $resulta in the main code and failed to update the $user_id line. I did update the $user_id line with $resulta and still have the same problem. This is consistent with the convention that $result was a local variable in the function and wasn't influencing the main code. I'm stumped. Link to comment https://forums.phpfreaks.com/topic/190732-losing-array-on-function-return/#findComment-1005840 Share on other sites More sharing options...
akitchin Posted February 3, 2010 Share Posted February 3, 2010 do you mean $resulta becomes a string? if so, have you tried making search_username() pass back $result->fields instead of the $result object? Link to comment https://forums.phpfreaks.com/topic/190732-losing-array-on-function-return/#findComment-1005862 Share on other sites More sharing options...
runsum Posted February 3, 2010 Author Share Posted February 3, 2010 Thanks akitchin, Your thoughts are appreciated. Not sure what the error was. Changed variable back to $result in both function and main code this morning and everything works fine. Link to comment https://forums.phpfreaks.com/topic/190732-losing-array-on-function-return/#findComment-1006213 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.