Jump to content

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


dottedquad

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?
Link to comment
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.
Link to comment
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
Link to comment
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?
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.