/    Sign up×
Community /Pin to ProfileBookmark

function Proble

Hi

I’m trying to write a function that will query a table and then store the values in a variable. if I use the below code on its own it works fine with no errors. nothing happens when I try and use the set variables on the page that I call the function.
calling the function

[code]
get_member_information(“first_name, last_name, email_address, phone_number, street_address, city, province, postal_code, last_login”);
[/code]

here is the function this far

[code]
function get_member_information($get_data)
{
include (“db.php”);
$member_id = ‘0000000’/*$_SESSION[‘user_id’]*/;

$sql = (“SELECT $get_data FROM members WHERE user_id =’$member_id'”);
echo $sql;

$qry = mysql_query($sql)or die(“SQL Error: $sql<br>” . mysql_error());

$member_details = mysql_fetch_assoc($qry);
echo $first_name;

$first_name = $member_details[‘first_name’];
$last_name = $member_details[‘last_name’];
$email_address = $member_details[’email_address’];
$street_address = $member_details[‘street_address’];
$city = $member_details[‘city’];
$provice = $member_details[‘provice’];
$postal_code = $member_details[‘postal_code’];
$signup_date = $member_details[‘signup_date’];
$new_login = $member_details[‘new_login’];
$last_login = $member_details[‘last_login’];
$user_level = $member_details[‘user_level’];
$activated = $member_details[‘activated’];
$event_id = $member_details[‘event_id’];
}
[/code]

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@NogDogMar 29.2009 — Variables within a function are local only to that function, except for the super-global arrays (such as $_POST or $_SESSION). What you probably want to do is assign all those values to an array, and then return it to the calling code.
[code=php]
function exampeFunction($id)
{
$id = (int) $id;
$data = array(); // this will hold the return data
$result = mysql_query("SELECT * FROM some_table WHERE id = $id");
if($result != false and mysql_num_rows($result))
{
$data = mysql_fetch_assoc($result);
}
return $data;
}

// main code
$data = exampleFunction($someValue);
if(!empty($data))
{
echo "<p>Hello, " . $data['first_name'] . "</p>n";
}
[/code]
×

Success!

Help @kproc spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.27,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...