Page 1 of 1

I have a variable scope problem

Posted: Sun Dec 10, 2006 7:41 am
by swiftouch
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I have a scope problem:

my function is:

Code: Select all

function getRelative($_PRIDN) {
	global $table;
	if ($_PRIDN != "") {
	$rel_sql = "SELECT first_name,mname1,last_name FROM $table WHERE PRIDN='$_PRIDN'";
	$rel_query = mysql_query($rel_sql)
		or die(mysql_error());
	$rel_row = mysql_fetch_array($rel_query)
		or die(mysql_error());
	}
}
My question is: How do I get the $rel_row array to be global so i can access the data outside of the function?
$rel_row = mysql_fetch_array($rel_query)

I've tried this: global $rel_row = mysql_fetch_array($rel_query) and it doesn't work.
I've tried this: global $rel_row; and it doesn't work.

I'll keep at it and hopefully find a way. Any help would be appreciated.
Grassious Mucho...in advance.

Swiftouch


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Sun Dec 10, 2006 8:05 am
by neophyte
The function needs to return a value.

Code: Select all

function getRelative($_PRIDN) 
{
  global $table;
  if ($_PRIDN != "") {
       $rel_sql = "SELECT first_name,mname1,last_name FROM $table WHERE PRIDN='$_PRIDN'";
       $rel_query = mysql_query($rel_sql) or die(mysql_error());
       $rel_row = mysql_fetch_array($rel_query) or die(mysql_error());
      return $rel_row;
  }
}
$rel_row = getRelative($_PRIDN);
print_r($rel_row);
I think that'll do it.