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? Quote Link to comment 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? Quote Link to comment 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. Quote Link to comment 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? Quote Link to comment 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. Quote Link to comment 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.