I have a variable scope problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
swiftouch
Forum Commoner
Posts: 80
Joined: Sun Dec 10, 2006 7:35 am
Location: Salt Lake City, Utah

I have a variable scope problem

Post 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]
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

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