Jump to content

Losing array on function return


runsum

Recommended Posts

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

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.