Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Returning mysql array values via function. 1

Status
Not open for further replies.

ag1060

Programmer
Aug 26, 2011
27
US
Hello,

I'm trying to return mysql values via function.

basically I have these two:

function create_project() {

load_user();

echo "Hello $uname ($email)";
exit;
}


function load_user() {
$id=$_SESSION['uid'];

$sql = <<<EOF
SELECT uname,email from x_members_x where uid='$id'
EOF;

$result = mysql_query($query) or die(mysql_error());




while($row = mysql_fetch_array($result)){
$uname=$row[0];
$email=$row[1];
}
//I wanna return all the values of uname/email.
}

}
I tried using the "return" statement but no luck :(

any help?

-aaron
 
#1. Always show the actual code, and not interpretation of it,. This code has many things wrong with it. but I doubt this is what your actual code looks like.


#2. Using returnr means the function "returns" a value but you still need to capture that value as it doesn't automagically create a variable with the returned value. So the correct usage of a function that returns a value would be:

Code:
$myvar = functionName();

or

echo functionName();

Then the returned value would appear in the $myvar variable, or be printed out to screen.


#3. Your while loop is overwriting your variables, since well they aren't arrays. If your query returns 5 results your only going to get the 5th and last set.

For easy management, I suggest making a multideimensional array and returning that.

Code:
$queryData[]="";
while($row = mysql_fetch_array($result)){

$queryData[]['uname']=$row[0];
$queryData[]['email']=$row[1];
}

return $queryData;
}

Then you can have your function return the results and use them as needed.

Code:
$data=load_user();

echo $data[0]['uname'];
echo $data[0]['email'];
echo $data[1]['uname'];
...
echo $data[3]['email'];

If your array is only every going to return one email and one usen, then just remove the initial [] so the array holds both elements in a single dimension.



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
Here's a version I use for exactly that. I wrote it specifically for what I need but may be of some use to others. It returns an array of results if there is more than one result from a SELECT query or a single value if there is only one result. If the value is an aray, you can also condense the array to get rid of the encapsulating array. Be aware that because this function could return two different types, you have to be careful how the result is exercised.


-Geates

"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top