1

Can you do that? I just tried but it doesnt seem to work.

I have dbc.php included at top of my page, and in dbc.php at the bottom i created this function:

function getUserInfo($id) {
$thaString = mysql_query("SELECT * FROM users WHERE id = '$id'");
$thaString2 = mysql_query("SELECT * FROM users_profile WHERE uID = '$id'");
$showUR = mysql_fetch_array($thaString);
$showURP = mysql_fetch_array($thaString2);
}

So it would be easier for me to call them instead of running the queries all the time when i need them in other pages.. But when i try to do:

getUserInfo($showInfo["bID"]);
echo $showUR["full_name"];

I dont get any result, is there a smarter way to do this, if so how?

4 Answers 4

2

It's an issue of scope: $showUR is set inside getUserInfo(), so it's not available to the echo outside the function. There are lots of potential modifications you could make, but you may want to assign your values into an array and then return that array:

function getUserInfo($id) {
    $user = array();

    $thaString = mysql_query("SELECT * FROM users WHERE id = '$id'");
    $thaString2 = mysql_query("SELECT * FROM users_profile WHERE uID = '$id'");
    $user['showUR'] = mysql_fetch_array($thaString);
    $user['showURP'] = mysql_fetch_array($thaString2);

    return $user;
}

$user = getUserInfo($showInfo["bID"]);
echo $user['showUR']["full_name"];
Sign up to request clarification or add additional context in comments.

Comments

1

Your functions have to return something for the values to be used, or $showUR and $showURP will just get lost once the function exits (ie: the scope will change). Something like:

function someFunc($arg) {
    return "Hello, I am {$arg}";
}

$showUR = someFunc('name');
echo $showUR;

And please don't call stuff $thaString. First because it's a misnomer (mysql_query() doesn't return a string, it returns a resource or a boolean), Second because "tha" is so lame.

Comments

0

Let your function return the variable.

And then use $showUR = getUserInfo(...)

Comments

0

If you declare them outside the function, and then as globals inside the function you should be able to use them as you are now.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.