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
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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.