dottedquad Posted July 9, 2006 Share Posted July 9, 2006 function code:[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]; }[/code]How do I access $fname, $lname, and $dob outside of the chk_member function so I echo out the values of those variables? Quote Link to comment https://forums.phpfreaks.com/topic/14066-trying-to-access-variables-that-are-inside-a-function/ Share on other sites More sharing options...
ShiVer Posted July 9, 2006 Share Posted July 9, 2006 I've always thought you chould just echo them. Can you not? Quote Link to comment https://forums.phpfreaks.com/topic/14066-trying-to-access-variables-that-are-inside-a-function/#findComment-54988 Share on other sites More sharing options...
dottedquad Posted July 9, 2006 Author Share Posted July 9, 2006 [quote author=ShiVer link=topic=99918.msg393790#msg393790 date=1152406386]I've always thought you chould just echo them. Can you not?[/quote]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. Quote Link to comment https://forums.phpfreaks.com/topic/14066-trying-to-access-variables-that-are-inside-a-function/#findComment-54993 Share on other sites More sharing options...
kenrbnsn Posted July 9, 2006 Share Posted July 9, 2006 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:[code]<?phplist ($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]));}?>[/code]Ken Quote Link to comment https://forums.phpfreaks.com/topic/14066-trying-to-access-variables-that-are-inside-a-function/#findComment-54998 Share on other sites More sharing options...
dottedquad Posted July 9, 2006 Author Share Posted July 9, 2006 [quote author=kenrbnsn link=topic=99918.msg393801#msg393801 date=1152408061]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:[code]<?phplist ($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]));}?>[/code]Ken[/quote]sweet that works, is there any way I can break up the those variables to echo them out seperatly? Quote Link to comment https://forums.phpfreaks.com/topic/14066-trying-to-access-variables-that-are-inside-a-function/#findComment-54999 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.