Jump to content

Archived

This topic is now archived and is closed to further replies.

dottedquad

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

Recommended Posts

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?

Share this post


Link to post
Share on other sites
I've always thought you chould just echo them. Can you not?

Share this post


Link to post
Share on other sites
[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.

Share this post


Link to post
Share on other sites
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]<?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]));
}
?>[/code]

Ken

Share this post


Link to post
Share on other sites
[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]<?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]));
}
?>[/code]

Ken
[/quote]

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

Share this post


Link to post
Share on other sites

×

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.