Jump to content


Photo

trying to access variables that are inside a function...


  • Please log in to reply
4 replies to this topic

#1 dottedquad

dottedquad
  • Members
  • Pip
  • Newbie
  • 9 posts

Posted 09 July 2006 - 12:12 AM

function code:
  function chk_member()
  {
    
	$location = "localhost";
    $username = "root";
    $password = "2dollarbill";
    $database = "gold";
	
	$conn = mysql_connect($location, $username, $password);
    if (!$conn) { die ("Could not connect to MySQL"); }
	mysql_select_db($database,$conn) or die ("Could not open database");
	
	$sql = mysql_query("SELECT `memid`, `fname`, `lname` FROM `membership` WHERE `memid` = 1234567891");
	$row = mysql_fetch_row($sql);
	
	$fname = $row[0];
	$lname = $row[1];
	$dob = $row[3];
	
 }

How do I access $fname, $lname, and $dob outside of the chk_member function so I echo out the values of those variables?

#2 ShiVer

ShiVer
  • Members
  • PipPip
  • Member
  • 14 posts
  • LocationJoMo

Posted 09 July 2006 - 12:53 AM

I've always thought you chould just echo them. Can you not?

#3 dottedquad

dottedquad
  • Members
  • Pip
  • Newbie
  • 9 posts

Posted 09 July 2006 - 01:02 AM

I've always thought you chould just echo them. Can you not?


Ya I can echo out the variables inside of the function, but i'm trying to echo them outside of the function.  It won't since i'm outside of the function scope.

#4 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 09 July 2006 - 01:21 AM

You either have to return them back to the calling scope or declare them global inside your routine. I prefer to return them in an array:
<?php
list ($fname, $lname, $dob) = chk_member();

  function chk_member()
  {
    
	$location = "localhost";
    $username = "root";
    $password = "2dollarbill";
    $database = "gold";
	
	$conn = mysql_connect($location, $username, $password);
    if (!$conn) { die ("Could not connect to MySQL"); }
	mysql_select_db($database,$conn) or die ("Could not open database");
	
	$sql = mysql_query("SELECT `memid`, `fname`, `lname` FROM `membership` WHERE `memid` = 1234567891");
	$row = mysql_fetch_row($sql);
         return (array($row[0], $row[1], $row[3]));
}
?>

Ken

#5 dottedquad

dottedquad
  • Members
  • Pip
  • Newbie
  • 9 posts

Posted 09 July 2006 - 01:24 AM

You either have to return them back to the calling scope or declare them global inside your routine. I prefer to return them in an array:

<?php
list ($fname, $lname, $dob) = chk_member();

  function chk_member()
  {
    
	$location = "localhost";
    $username = "root";
    $password = "2dollarbill";
    $database = "gold";
	
	$conn = mysql_connect($location, $username, $password);
    if (!$conn) { die ("Could not connect to MySQL"); }
	mysql_select_db($database,$conn) or die ("Could not open database");
	
	$sql = mysql_query("SELECT `memid`, `fname`, `lname` FROM `membership` WHERE `memid` = 1234567891");
	$row = mysql_fetch_row($sql);
         return (array($row[0], $row[1], $row[3]));
}
?>

Ken


sweet that works, is there any way I can break up the those variables to echo them out seperatly?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users